note 75714 modified in function.str-replace by ronabop
| From: | ronabop@php.net | Date: | Wed, 13 Jun 2007 03:52:23 +0000 |
| Subject: | note 75714 modified in function.str-replace by ronabop | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127434@lists.php.net to get a copy of this message | ||
Here is a version that allows for empty multidimensional arrays:
function str_replace_array ($search, $replace, $subject) {
if (is_array($subject)) {
foreach ($subject as $id=>$inner_subject) {
$subject[$id]=str_replace_array($search, $replace, $inner_subject);
}
} else {
$subject=str_replace($search, $replace, $subject);
}
return $subject;
}
--was--
The function provided by Toby has a slight flaw.
If you start with $arr=array(1=>array('Flowers','Candy'),2=>array())
and perform str_replace_array('Flowers','Flower',$arr)
You will end up with only array(1=>array('Flower','Candy'))
Here is a version that allows for empty arrays:
function str_replace_array ($search, $replace, $subject) {
if (is_array($subject)) {
foreach ($subject as $id=>$inner_subject) {
$subject[$id]=str_replace_array($search, $replace, $inner_subject);
}
} else {
$subject=str_replace($search, $replace, $subject);
}
return $subject;
}
http://php.net/manual/en/function.str-replace.php