是否有内置的PHP函数用一个数组替换字符串中的多个值,该数组确切地说明什么被替换为什么?
例如:
$searchreplace_array = Array('blah' => 'bleh', 'blarh' => 'blerh'); $string = 'blah blarh bleh bleh blarh';
结果将是:'bleh blerh bleh bleh blerh'.
你在找str_replace()
.
$string = 'blah blarh bleh bleh blarh'; $result = str_replace( array('blah', 'blarh'), array('bleh', 'blerh'), $string );
//补充提示:
如果您在示例中遇到关联数组,则可以将其拆分为:
$searchReplaceArray = array( 'blah' => 'bleh', 'blarh' => 'blerh' ); $result = str_replace( array_keys($searchReplaceArray), array_values($searchReplaceArray), $string );
$string = 'blah blarh bleh bleh blarh'; $trans = array("blah" => "blerh", "bleh" => "blerh"); $result = strtr($string,$trans);
您可以查看手册以获取详细说明.