Latest web development tutorials

PHP strtr () function

PHP String Reference PHP String Reference

Examples

The characters in the string "ia" replace "eo":

<?php
echo strtr("Hilla Warld","ia","eo");
?>

Running instance »

Definition and Usage

strtr () function converts a specific character string.

Note: If the length from and to different parameters, the format for the shortest length.


grammar

strtr( string,from,to )

or

strtr( string,array )

参数 描述
string 必需。规定要转换的字符串。
from 必需(除非使用数组)。规定要改变的字符。
to 必需(除非使用数组)。规定要改变为的字符。
array 必需(除非使用 from 和 to)。一个数组,其中的键名是原始字符,键值是目标字符。

technical details

return value: Returns converted character.
If the array parameter contains an empty string ( "") key name, it returns FALSE.
PHP version: 4+


More examples

Example 1

The string "Hello world" replaced "Hi earth":

<?php
$arr = array("Hello" => "Hi", "world" => "earth");
echo strtr("Hello world",$arr);
?>

Running instance »


PHP String Reference PHP String Reference