我在做字符串的截取的时候很纠结,因为发现网络上,用的substr_replace()等等,根本没有作用。
后来,TK介绍说strtr这个函数很好用:
strtr — 转换指定字符
string
strtr ( string
$str , string
$from , string
$to )
string
strtr ( string
$str , array
$replace_pairs )
参数 replace_pairs 可以用来取代 to 和 from 参数,因为它是以 array('from' => 'to', ...) 格式出现的数组。
<?php
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
?>