php replace from multidimensional array -
i looking way in php can replace string this
<?php $ar = array( "tofind" => "tobereplaced", "tofind1" => "tobereplaced1" ); ?> and on ?
any ? thanks
simple replacement tasks can done using str_replace:
$string = str_replace(array_keys($ar), array_values($ar), $string); as noted in examples, may lead unexpected behaviour because replacement performed left-to-right, ie. output of first replacement acts input second replacement , on. comments on php manual page str_replace contain lot of ideas replacing occurrences in original string. this one, example, might close you're looking for:
function stro_replace($search, $replace, $subject) { return strtr($subject, array_combine($search, $replace)); } $search = array('erica', 'america'); $replace = array('jon', 'php'); $subject = 'mike , erica america'; echo str_replace($search, $replace, $subject); // output: "mike , jon amjon", not correct echo stro_replace($search, $replace, $subject); // output: "mike , jon php", correct
Comments
Post a Comment