note 18079 deleted from function.preg-replace by sniper

From: Date: Sun, 07 Sep 2003 21:58:42 +0000
Subject: note 18079 deleted from function.preg-replace by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-55675@lists.php.net to get a copy of this message
Note Submitter: bisqwit@iki.fi ---- 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).

« previous php.notes (#55675) next »