#23654 [Asn]: for nested arryas, str_replace return wrong value
| From: | andrey@php.net | Date: | Fri, 16 May 2003 14:55:11 +0000 |
| Subject: | #23654 [Asn]: for nested arryas, str_replace return wrong value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-39794@lists.php.net to get a copy of this message | ||
ID: 23654
Updated by: andrey@php.net
Reported By: marcel_preda at yahoo dot com
Status: Assigned
Bug Type: Scripting Engine problem
Operating System: any
PHP Version: 4.3.1
-Assigned To: andrey
+Assigned To: pollita
New Comment:
Pollita is keen on str_replace() :)
Previous Comments:
------------------------------------------------------------------------
[2003-05-16 02:36:50] moriyoshi@php.net
assigning to andrey
------------------------------------------------------------------------
[2003-05-16 02:13:20] 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;
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=23654&edit=1