#49569 [NEW]: preg_replace global replace bug

From: Date: Wed, 16 Sep 2009 10:42:44 +0000
Subject: #49569 [NEW]: preg_replace global replace bug
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-142408@lists.php.net to get a copy of this message
From: thespiraloflife at hotmail dot com Operating system: linux PHP version: 5.2.10 PHP Bug Type: Regexps related Bug description: preg_replace global replace bug Description: ------------ preg_replace has a bug //CASE STUDY: remove multiple instances of &nbsp; inside <table> <div id="Paginator">&nbsp; <span></span>&nbsp;<a href="#"></a> &nbsp; </div></table> $m = "¬";//Any marker that isn't currently in the strHTML string. Note, this is an example, but you should use a function that gets a safeMarker. // replaces all instances of </table> with </table>¬ $strHTML = preg_replace('/(<\/table>)/','$1'.$m, $strHTML); //looks for all &nbsp; within <table><div id="Paginator"> and removes them //This would be the code to use but php seems to be greedy and doesn't match all instances and only gets the last &nbsp;, where as perl, .NET or javascript regular expressions would $strHTML = preg_replace('/(\<div\sid\=\"Paginator\"\>[^\¬]*)\&nbps\;/','$1', $strHTML); Reproduce code: --------------- --- From manual page: function.preg-replace --- //CASE STUDY: remove multiple instances of &nbsp; inside <table> <div id="Paginator"></div></table> $m = "¬";//Any marker that isn't currently in the strHTML string. Note, this is an example, but you should use a function that gets a safeMarker. // replaces all instances of </table> with </table>¬ $strHTML = preg_replace('/(<\/table>)/','$1'.$m, $strHTML); //looks for all &nbsp; within <div id="Paginator"> and removes them //This would be the code to use but php seems to be greedy and doesn't match all instances and only gets the last &nbsp;, where as perl, .NET or javascript regular expressions would $strHTML = preg_replace('/(\<div id\=\"Paginator\"\>[^\¬]*)\&nbps\;/','$1', $strHTML); Expected result: ---------------- Would remove all instances of &nbsp; inside <div id="Paginator" Actual result: -------------- Only removes the last &nbps; inside <div id="Paginator" -- Edit bug report at http://bugs.php.net/?id=49569&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49569&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49569&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49569&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=49569&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49569&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=49569&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=49569&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=49569&r=needscript Try newer version: http://bugs.php.net/fix.php?id=49569&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=49569&r=support Expected behavior: http://bugs.php.net/fix.php?id=49569&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=49569&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=49569&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=49569&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49569&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=49569&r=dst IIS Stability: http://bugs.php.net/fix.php?id=49569&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=49569&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=49569&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=49569&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=49569&r=mysqlcfg

« previous php.bugs (#142408) next »