#23654 [Asn->Csd]: for nested arryas, str_replace return wrong value
| From: | pollita@php.net | Date: | Fri, 16 May 2003 23:35:19 +0000 |
| Subject: | #23654 [Asn->Csd]: for nested arryas, str_replace return wrong value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-39821@lists.php.net to get a copy of this message | ||
ID: 23654
Updated by: pollita@php.net
Reported By: marcel_preda at yahoo dot com
-Status: Assigned
+Status: Closed
Bug Type: Scripting Engine problem
Operating System: any
PHP Version: 4.3.1
Assigned To: pollita
New Comment:
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).
Previous Comments:
------------------------------------------------------------------------
[2003-05-16 09:55:10] andrey@php.net
Pollita is keen on str_replace() :)
------------------------------------------------------------------------
[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