#47657 [Bgs]: str_replace() should not call replace-functions when needle missing

From: Date: Sat, 14 Mar 2009 18:03:18 +0000
Subject: #47657 [Bgs]: str_replace() should not call replace-functions when needle missing
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-134701@lists.php.net to get a copy of this message
ID: 47657 User updated by: h dot reindl at thelounge dot net Reported By: h dot reindl at thelounge dot net Status: Bogus Bug Type: Performance problem PHP Version: 5.2.9 New Comment: And you think it is best practice every possible optimzing that is suggestet by users throw against a wall? Sorry but for me it seems since a long time that php-developers are not interested in any help and thinking about making things better from the community Previous Comments: ------------------------------------------------------------------------ [2009-03-14 17:53:07] jani@php.net str_replace() does not accept functions as the parameters like you expect it to so there is no bug here either. ------------------------------------------------------------------------ [2009-03-14 17:41:10] h dot reindl at thelounge dot net 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 this bug report at http://bugs.php.net/?id=47657&edit=1

« previous php.bugs (#134701) next »