strtr
翻译字符或替换子字符串-转换指定字符
strtr()
函数转换字符串中特定的字符。
注释:如果 from 和 to 参数的长度不同,则会被格式化为最短的长度。
把字符串中的字符 "ia" 替换为 "eo":
<?php echo strtr("Hilla Warld","ia","eo"); ?>
亲自试一试
把字符串 "Hello world" 替换成 "Hi earth":
<?php $arr = array("Hello" => "Hi", "world" => "earth"); echo strtr("Hello world",$arr); ?>
亲自试一试
strtr(string,from,to)
或者:
strtr(string,array)
参数 | 描述 |
---|---|
string | 必需。规定要转换的字符串。 |
from | 必需(除非使用数组)。规定要改变的字符。 |
to | 必需(除非使用数组)。规定要改变为的字符。 |
array | 必需(除非使用 from 和 to)。数组,其中的键名是更改的原始字符,键值是更改的目标字符。 |