note 95198 added to function.str-replace
| From: | WesFoster at osu1 dot php dot net | Date: | Thu, 17 Dec 2009 21:32:02 +0000 |
| Subject: | note 95198 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164096@lists.php.net to get a copy of this message | ||
Feel free to optimize this using the while/for or anything else, but this is a bit of code that
allows you to replace strings found in an associative array.
For example:
<?php
$replace = array(
'dog' => 'cat',
'apple' => 'orange'
'chevy' => 'ford'
);
$string = 'I like to eat an apple with my dog in my chevy';
echo str_replace_assoc($replace,$string);
// Echo: I like to eat an orange with my cat in my ford
?>
Here is the function:
<?php
function str_replace_assoc($array,$string){
$from_array = array();
$to_array = array();
foreach ($array as $k => $v){
$from_array[] = $k;
$to_array[] = $v;
}
return str_replace($from_array,$to_array,$string);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 72.160.54.241
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/95198
Del: integrated -- https://master.php.net/note/delete/95198/integrated
Del: useless -- https://master.php.net/note/delete/95198/useless
Del: bad code -- https://master.php.net/note/delete/95198/bad+code
Del: spam -- https://master.php.net/note/delete/95198/spam
Del: non-english -- https://master.php.net/note/delete/95198/non-english
Del: in docs -- https://master.php.net/note/delete/95198/in+docs
Del: other reasons-- https://master.php.net/note/delete/95198
Reject -- https://master.php.net/note/reject/95198
Search -- https://master.php.net/manage/user-notes.php