0

How can I replace part of a string, when that part and the replacement both include special characters? e.g.

$text = "|123|12|12|";
$text = preg_replace("|0|","|12|",$text, 1);
echo($text);

Desired output: "|123|0|12|"

The special characters don't matter as long as they are preserved. E.g.

$text = "#123#12#12#";
$text = preg_replace("#0#","#12#",$text, 1);
echo($text);

Desired output: "#123#0#12#"

Any ideas?

3 Answers 3

3

Use str_replace instead of preg_replace if you don't need regexp. It's more efficient and avoids the need to escape anything.

If you need to use preg_replace (for instance to take advantage of the fact that you can specify a limit for the number of replacements, unlike with str_replace), use preg_quote to escape the special characters.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But how do I only replace the first occurrence? str_replace seems to want to change everything.
@Chris: So does preg_replace, actually.
1

The | acts as the marker for the start of the regex. You're quite close really:

$text = "|123|12|12|";
$text = preg_replace("/|0|/","|12|",$text, 1);
echo($text);

Here I used / as the start and end-markers. You can use any character. / is quite common.

Note that str_replace would also suffice here

5 Comments

Thanks. I tried that in writecodeonline.com/php and it came up with /|12|/|123|12|12|
Even after fixing the backwards pattern and replacement strings, this will result in something like |12|||12|1|12|2|12|3|12|||12|... The | means "or", so the pattern will match the empty string between each char unless the |s are escaped.
Sorry for being dense, but it doesn't work for me. e.g. $text = "|123|12|12|"; $text = preg_replace("\|0\|","\|12\|",$text, 1); echo($text); the output is |123|12|12| - how do I escape these characters?
@Chris: Your pattern needs chars before and after it. Evert chose / (which is the most common). Your pattern doesn't have those. But with that fixed, you're trying to find |0| (which doesn't exist in the string). Reverse the |0| and |12|, and you're a tiny bit closer (but you'll still have problems with the last |12| getting replaced as well).
Thanks. I'm more confused than ever - I think I'll just send the input to the local machine, process it in javascript and send it back. PHP seems like a black art.
0

Your pattern needs escapement,because the '|' is a special char in reg rules. Use '\' to escape your char, and then use '/' as the delimiter of your pattern. Try this. :)

$text = "|123|12|12|";
$text = preg_replace("/\|12\|/","|0|",$text, 1);
echo($text);

EDIT: BTY, I guess you want to replace the first '|12|' with '|0|', but your code makes me puzzled. The function preg_replace's statement is as below:

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

Searches subject for matches to pattern and replaces them with replacement.

Maybe you had put the parameters in wrong places.
See more about preg_replace: http://cn.php.net/manual/en/function.preg-replace.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.