note 40697 rejected from function.preg-replace by nlopess
| From: | nlopess@php.net | Date: | Thu, 11 Mar 2004 17:42:12 +0000 |
| Subject: | note 40697 rejected from function.preg-replace by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-66673@lists.php.net to get a copy of this message | ||
Note Submitter: php@equaliser.net
----
I suspect I've found a bug here - but here goes:
I can't seem to escape dollar signs ($) in replacement syntax for preg_replace.
Let's say I've got three strings coming into my preg_replace from elsewhere:
$string = 'Preg replace WORKS very well in these circumstances';
$pattern = "!WORKS!";
$replacement = '$1 DOES NOT WORK';
Then doing:
echo preg_replace($pattern , $replacement , $string);
Echos: 'Preg replace DOES NOT WORK very well in these circumstancesPreg' with no dollar
sign.
So let's try:
$replacement = preg_replace("!\$!" , "\x24" , $replacement);
echo preg_replace($pattern , $replacement , $string);
Echos: 'Preg replace DOES NOT WORK$ very well in these circumstancesPreg'
So let's try:
$replacement = preg_replace("!\x24!" , "\x24" , $replacement);
Echos : 'Preg replace DOES NOT WORK$ very well in these circumstancesPreg'
Okay then, how about:
$replacement = preg_replace("!" . '\x24' . "!" , "\x24" ,
$replacement);
Echos : 'Preg replace DOES NOT WORK very well in these circumstancesPreg'
RIGHT then - finally.
$replacement = preg_replace("!" . '\x24' . "!" , '\x24' ,
$replacement);
Echos : 'Preg replace \x241 DOES NOT WORK very $ well in these circumstances'
--
Oh dear... Is there any way I can put a dollar sign followed by a number into a regular expression,
and get one back out? OK - preg_quote() works, but it also screws up the rest of the replacement
string, and there's no preg_unquote() is there?