This will result in a string search for each replacement in order. If he finds one, he will split the string and look for the rest of the string for any other replacements.
$str = '012abc.d+e_fg~hijk'; $rep = array( 'st' => '{pre}', 'abc' => '{start}', 'e' => '{middle}', 'hi' => '{end}', 'dd' => '{post}' ); $searched = ''; foreach ($rep as $key => $r) { if (strpos($str, $key) !== false) { $searched .= substr($str, 0, strpos($str, $key)) . $r; $str = substr($str, strpos($str, $key) + strlen($key)); } } $searched .= $str; echo $searched;
He will search and find them in the order you specify.
source share