php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33773 str_replace recursion
Submitted: 2005-07-19 18:14 UTC Modified: 2005-07-19 20:04 UTC
From: tomlove at gmail dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.3.11 OS: Linux / Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
25 + 15 = ?
Subscribe to this entry?

 
 [2005-07-19 18:14 UTC] tomlove at gmail dot com
Description:
------------
I assume this is a feature: str_replace recursively replaces when both $search and $replace are arrays.

It's too easy for this to cause runaway memory consumption though. 

I've never needed to take advantage of the recursive nature of the function, and wouldn't expect many others to.

The code below is enough to hang my system. 

Reproduce code:
---------------
for ($i = 0; $i < 50; $i++) {
  $arr1[$i] = "foo";
  $arr2[$i] = "foobarfoo";
}

$str = "foobar";
echo str_replace($arr1, $arr2, $str);

Expected result:
----------------
Ideally:
foobarfoobar

Actual result:
--------------
foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar.... 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-19 20:04 UTC] tomlove at gmail dot com
Actually, I realise it wouldn't be a trivial to change this behaviour. Probably better to leave checks for recursive cases to the script.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 24 05:01:32 2024 UTC