note 18079 added to function.preg-replace
| From: | bisqwit at iki dot fi | Date: | Wed, 09 Jan 2002 23:25:27 +0000 |
| Subject: | note 18079 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-24599@lists.php.net to get a copy of this message | ||
If you're using preg_replace with callbacks, you must use the following method to get the
matched string parts unmodified to the callback function:
function handler($s) { print str_replace('\"', '"', $s); }
$s = "\"Don't cause problems, m'kay?\"";
print "$s\n";
preg_replace('/^(.*)/e', "handler('\\1')", $s);
Point here being, that you must use str_replace in the callback function (to replace \" with
"), and the function parameters in singlequotes (to get $-signs unmodified).
preg_replace replaces ' with \', \ with \\ and " with \" when parsing the \digit
or $digit -sequences in the replacement string.
If you omit str_replace, you get extra backslashes, and if you use
'handler("\1")', you get problems with input containing $-signs (plus extra
backslashes if you don't do proper str_replace).
--
http://www.php.net/manual/en/function.preg-replace.php
http://master.php.net/manage/user-notes.php?action=edit+18079
http://master.php.net/manage/user-notes.php?action=delete+18079
http://master.php.net/manage/user-notes.php?action=reject+18079