note 40697 added to function.preg-replace

From: Date: Thu, 11 Mar 2004 17:23:13 +0000
Subject: note 40697 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-66670@lists.php.net to get a copy of this message
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? ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40697 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40697&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40697&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#66670) next »