#47657 [NEW]: str_replace should not call replace-functions when needle missing

From: Date: Sat, 14 Mar 2009 17:41:10 +0000
Subject: #47657 [NEW]: str_replace should not call replace-functions when needle missing
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-134699@lists.php.net to get a copy of this message
From: h dot reindl at thelounge dot net Operating system: PHP version: 5.2.9 PHP Bug Type: Performance problem Bug description: str_replace should not call replace-functions when needle missing Description: ------------ If you use str_replace and the needle is not in the string there should be no call to the replacement-function. This can produce much overhead if the function is complex At the moment you must manually check if you call the replace with strpos - I think in many cases this will not happen and there is running much overhead-code without any need in existing applications Even if you know the problem it needs ugly php-code for get best performance - If the php-core makes this check all exististing applications can get better performance Reproduce code: --------------- <?php $str = 'Test [f1] Replace'; $str = str_replace('[f1]', f1(), $str); $str = str_replace('[f2]', f2(), $str); function f1() { echo 'F1<br />'; } function f2() { echo 'F2<br />'; } ?> Expected result: ---------------- Only f1() should be called Actual result: -------------- f1() AND f2() are called -- Edit bug report at http://bugs.php.net/?id=47657&edit=1 -- Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=47657&r=trysnapshot52 Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=47657&r=trysnapshot53 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=47657&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=47657&r=fixedcvs Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=47657&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=47657&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=47657&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=47657&r=needscript Try newer version: http://bugs.php.net/fix.php?id=47657&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=47657&r=support Expected behavior: http://bugs.php.net/fix.php?id=47657&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=47657&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=47657&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=47657&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=47657&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=47657&r=dst IIS Stability: http://bugs.php.net/fix.php?id=47657&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=47657&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=47657&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=47657&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=47657&r=mysqlcfg

« previous php.bugs (#134699) next »