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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tomlove at gmail dot com
New email:
PHP Version: OS:

 

 [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: Wed Apr 24 11:01:30 2024 UTC