php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22141 str_replace hangs with extra argument
Submitted: 2003-02-09 15:15 UTC Modified: 2003-02-10 13:50 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: arlo at typea dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.2.3 OS: Mac OS 10.2
Private report: No CVE-ID:
 [2003-02-09 15:15 UTC] arlo at typea dot net
I was inadvertently including a fourth argument of "-1" 
with some instances of str_replace() -- since I got into 
the habit of the limit arg using preg_replace() -- and I 
noticed an odd behavior:

str_replace("-embed-a-", "hey", "-embed-a-", -1) returns 
"hey," as expected.

str_replace("-embed-a-", "hey", "-embed-b-", -1) returns 
"embed-b-," as expected.

But str_replace("-embed-a-", "hey", "-embed-aa-", -1) hangs 
the script until the max execution time.

Removing the extraneous argument made my script work 
properly, but I would expect PHP to either ignore the extra 
argument, or return an error, rather than hanging up 
completely.

-Arlo

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-10 00:50 UTC] pollita@php.net
This has already been fixed in CVS.  PHP's internals contain two str_replace methods, the fourth parameter (when set to a non-zero value) uses the alternate search and replace method.  

It's not documented because the alternate method was not endoresed for use.  As of 4.3.1 (which is yet to be released, you can use a snapshot from snaps.php.net in the mean time), the alternate method has been removed (and the existing method improved for speed and efficiency.
 [2003-02-10 13:50 UTC] pollita@php.net
One more thing.  It actually hadn't been removed in the 4.3 branch until just now as the method was not known to cause any actual problems and non-bug-fix changes are generally kept to the development branch.

I've gone ahead and merged in the relevant portion of the changes to str_replace.  PHP will now throw the customary "Incorrect Parameter Count" error if you use anything but 3 parameters with str_replace.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 17:01:54 2014 UTC