#23654 [NEW]: for nested arryas, str_replace return wrong value
| From: | marcel_preda at yahoo dot com | Date: | Fri, 16 May 2003 07:13:20 +0000 |
| Subject: | #23654 [NEW]: for nested arryas, str_replace return wrong value | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-39764@lists.php.net to get a copy of this message | ||
From: marcel_preda at yahoo dot com
Operating system: any
PHP version: 4.3.1
PHP Bug Type: Scripting Engine problem
Bug description: for nested arryas, str_replace return wrong value
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 bug report at http://bugs.php.net/?id=23654&edit=1
--
Try a CVS snapshot: http://bugs.php.net/fix.php?id=23654&r=trysnapshot
Fixed in CVS: http://bugs.php.net/fix.php?id=23654&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=23654&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=23654&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=23654&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=23654&r=support
Expected behavior: http://bugs.php.net/fix.php?id=23654&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=23654&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=23654&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=23654&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=23654&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=23654&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=23654&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=23654&r=gnused