php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23654 for nested arryas, str_replace return wrong value
Submitted: 2003-05-16 02:13 UTC Modified: 2003-05-16 18:36 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: marcel_preda at yahoo dot com Assigned: pollita (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 4.3.1 OS: any
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: marcel_preda at yahoo dot com
New email:
PHP Version: OS:

 

 [2003-05-16 02:13 UTC] marcel_preda at yahoo dot com
I suppose that the problem is in ALL PHP versions.

from PHP manual:
---
mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count])
---


If `subject' is a nested array the return is strange (at least unexpected)

For example, next pice of code:
<?php
$x = array ( 'abc', 'xayz',
        array('ddddadddd ', ' fffffffffff ' )
        );
$y=str_replace('a','A',$x);
print_r($y);
?>


I'm expecting as output:
Array
(
    [0] => Abc
    [1] => xAyz
    [2] => Array
        (
            [0] => ddddAdddd 
            [1] =>  fffffffffff 
        )

)

But, the output is:
Array
(
    [0] => Abc
    [1] => xAyz
    [2] => ArrAy
)

I thnik that str_replace should do a recursive call, with type checking.

For now I've solve the problem in my own way:
// for nested arrays as `subject' looks like str_replace has wrong return
function my_str_replace($search, $replace, $mixed)
{
        if (is_string($mixed))
                return str_replace($search, $replace, $mixed );
        if(is_array($mixed))
        {
                foreach($mixed AS $key=>$value)
                         $mixed[$key] = my_str_replace($search, $replace, $value);
                return $mixed;
        };
        // non interesting types
        return $mixed;
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-16 02:36 UTC] moriyoshi@php.net
assigning to andrey

 [2003-05-16 09:55 UTC] andrey@php.net
Pollita is keen on str_replace() :)
 [2003-05-16 18:35 UTC] pollita@php.net
This has been fixed in CVS, however not *quite* in the manner you stated as your expected output.  For subarrays in the subject array str_replace will not pass them through *untouched*.

In your example this means the output will now appear as:
Array
(
    [0] => Abc
    [1] => xAyz
    [2] => Array
        (
            [0] => ddddadddd 
            [1] =>  fffffffffff 
        )

)


A recursive str_replace was considered, however it was determined to be unwise at this stage of PHP 4.3's development cycle.  It is, however, under consideration for PHP 5.0 and will likely appear as either an additional parameter ($recursive : true/false) or a unique function (str_replace_recursive).
 [2003-05-16 18:36 UTC] pollita@php.net
heh.... typo....

str_replace
will not pass them through *untouched*.

should read:

str_replace
will pass them through *untouched*.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 07:01:27 2024 UTC