preg_replace does not replace all occurrences
| From: | Martin Scotta | Date: | Mon, 14 Mar 2011 15:18:12 +0000 |
| Subject: | preg_replace does not replace all occurrences | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-51682@lists.php.net to get a copy of this message | ||
I chose the simplest example to show the preg_replace behavior, there are
better (and safer) ways to scape slash characters.
Anyways, *is this the expected preg_replace behavior?*
Martin
<?php
function test($str) {
static $re = '/(^|[^\\\\])\'/';
static $change = '$1\\\'';
echo $str, PHP_EOL,
preg_replace($re, $change, $str), PHP_EOL, PHP_EOL;
}
test("str '' str"); // bug?
test("str \\'\\' str"); // ok
test("'str'"); // ok
test("\'str\'"); // ok
----
Expected:
str '' str
str \'\' str
str \'\' str
str \'\' str
'str'
\'str\'
\'str\'
\'str\'
----
Result:
str '' str
str \'' str
str \'\' str
str \'\' str
'str'
\'str\'
\'str\'
\'str\'
Martin Scotta