How to replace multiple values in php -
$srting = "test1 test1 test2 test2 test2 test1 test1 test2"; how can change test1 values test2 , test2 values test1 ?
when use str_replace , preg_replace values changed last array value. example:
$pat = array(); $pat[0] = "/test1/"; $pat[1] = "/test2/"; $rep = array(); $rep[0] = "test2"; $rep[1] = "test1"; $replace = preg_replace($pat,$rep,$srting) ; result:
test1 test1 test1 test1 test1 test1 test1 test1
this should work you:
<?php $string = "test1 test1 test2 test2 test2 test1 test1 test2"; echo $string . "<br />"; echo $string = strtr($string, array("test1" => "test2", "test2" => "test1")); ?> output:
test1 test1 test2 test2 test2 test1 test1 test2 test2 test2 test1 test1 test1 test2 test2 test1 checkout demo: http://codepad.org/b0db95x5
Comments
Post a Comment