#43532 [NEW]: preg_replace: Escaping \ and $n in 2nd arg
| From: | csaba at alum dot mit dot edu | Date: | Sat, 08 Dec 2007 02:03:51 +0000 |
| Subject: | #43532 [NEW]: preg_replace: Escaping \ and $n in 2nd arg | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-328@lists.php.net to get a copy of this message | ||
From: csaba at alum dot mit dot edu
Operating system:
PHP version: Irrelevant
PHP Bug Type: Documentation problem
Bug description: preg_replace: Escaping \ and $n in 2nd arg
Description:
------------
preg_replace("/x/", '\\\\', "axb")
=> a\b
This has been reported at least 5 times as a bug in PHP (bugs 11594,
12668, 17616, 34560, 10626), and each time the answer comes back that it's
not a bug in PHP, and to check the documentation.
There is nothing in the documentation to indicate this behaviour,
especially with no /e modifier, so the documentation should be revised. I
would suggest something like the below - edit to suit:
Csaba Gabor from Vienna
$replacement has two type of escaping: pairs of backslashes are combined,
and $n is a back reference to a captured (sub)pattern.
Example: To make a pure substitution without escaping effects, the
following may be used:
$subject = "xay";
$replacement = 'b\c\\d\\\e\\\\$0f\\\\\g';
// first one protects backslashes, 2nd one protects $
$aMap = array("/\\\\+/" => '$0$0', "/\\$/" =>
'\\\\$');
$rep = preg_replace(array_keys($aMap),
array_values($aMap),$replacement);
// protected replacement
$result = preg_replace("/(a)/", $rep, $subject);
// original, unprotected replacement
$res0 = preg_replace("/(a)/", $replacement, $subject);
$replacment => b\c\d\\e\\$0f\\\g
$rep => b\\c\\d\\\\e\\\\\$0f\\\\\\g
$result => xb\c\d\\e\\$0f\\\gy
$res0 => xb\c\d\e\af\\gy
--
Edit bug report at http://bugs.php.net/?id=43532&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=43532&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=43532&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=43532&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=43532&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=43532&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=43532&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=43532&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=43532&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=43532&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=43532&r=support
Expected behavior: http://bugs.php.net/fix.php?id=43532&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=43532&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=43532&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=43532&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=43532&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=43532&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=43532&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=43532&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=43532&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=43532&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=43532&r=mysqlcfg