Re: preg_replace does not replace all occurrences

From: Date: Mon, 14 Mar 2011 15:33:42 +0000
Subject: Re: preg_replace does not replace all occurrences
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-51683@lists.php.net to get a copy of this message
On 14 March 2011 15:18, Martin Scotta <martinscotta@gmail.com> wrote: > <?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 > Your regex is ... (^|[^\\\\])' Options: case insensitive; ^ and $ match at line breaks Match the regular expression below and capture its match into backreference number 1 «(^|[^\\\\])» Match either the regular expression below (attempting the next alternative only if this one fails) «^» Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» Or match regular expression number 2 below (the entire group fails if this one fails to match) «[^\\\\]» Match a single character NOT present in the list below «[^\\\\]» A \ character «\\» A \ character «\\» Match the character “'” literally «'» I think [^\\\\] is wrong and you want it to be ... (?!\\\\) or (?!\\{2}) With that, the output is ... str '' str str \'\' str str \'\' str str \\'\\' str 'str' \'str\' \'str\' \\'str\\' -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

« previous php.internals (#51683) next »