From: Operating system: Mac OS X 10.6 PHP version: 5.3.3 Package: PCRE related Bug Type: Bug Bug description:preg_replace should allow all special characters to be escaped
Description: ------------ One can use preg_quote() to prep a string for use as the search string with preg_replace() and family. However, attempting to pass a string escaped by preg_quote() into preg_replace() as the replacement string does not work because all PCRE-special characters (such as '+') are escaped, but only '$' and '\' are unescaped by preg_replace(). IOW, this: $result = preg_replace("/bar/", '\\\\BAR\$\+', 'foo bar baz'); yields: foo \BAR$\+ baz preg_replace() should treat all escaped characters equally so that one can simply call preg_quote() and be done with it. As it is now, one must do something like this: $safeReplacementString = str_replace(array('\\', '$'), array('\\\\', '\\$'), $replacementString); to avoid problems. Not is that an ugly solution, but I strongly suspect that most code out there doesn't do it. Expected result: ---------------- The preg_replace() family of functions should accept any escaped PCRE special character sequence in the replacement text and treat it like the literal equivalent. Thus, '\+' should be treated as the literal '+'. Actual result: -------------- Presently, the preg_replace() family of functions only accept escaped sequences for '\' and '$'. If other PCRE-special characters, such as '+' or '*', are passed into the replacement string, the escape sequences (e.g., '/+') are left intact in the output result. -- Edit bug report at http://bugs.php.net/bug.php?id=52962&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=52962&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=52962&r=trysnapshot53 Try a snapshot (trunk): http://bugs.php.net/fix.php?id=52962&r=trysnapshottrunk Fixed in SVN: http://bugs.php.net/fix.php?id=52962&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52962&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=52962&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=52962&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=52962&r=needscript Try newer version: http://bugs.php.net/fix.php?id=52962&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=52962&r=support Expected behavior: http://bugs.php.net/fix.php?id=52962&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=52962&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=52962&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=52962&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52962&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=52962&r=dst IIS Stability: http://bugs.php.net/fix.php?id=52962&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=52962&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=52962&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=52962&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=52962&r=mysqlcfg