|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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;
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 11:00:02 2025 UTC |
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).