Home > Article > Backend Development > How to use php strtr function
PHP strtr function is used to convert certain characters in a string. Its syntax is strtr (string, from, to) or strtr (string, array). The parameter string is required and specifies the string to be converted; from is required (Unless an array is used), it specifies the characters to be changed...
php How to use the strtr function?
php strtr() function syntax
Function: Convert certain characters in the string
Syntax:
strtr(string,from,to)或者strtr(string,array)
Parameters:
string required. Specifies the string to be converted.
from Required (unless using an array). Specifies the character to be changed.
to Required (unless using an array). Specifies the character to change to.
array Required (unless from and to are used). An array where the key is the original character changed and the key value is the target character changed.
Description:
Convert specific characters in the string. If the from and to parameters are of different lengths, they will be formatted to the shortest length.
php strtr() function usage example 1:
<?php //将字符串中的"i,a"替换成"e.o" echo strtr("Hilla php.cn","ia","eo"); echo "<br>"; //替换数组中的元素 $arr = array("Hello" => "Hi", "world" => "php.cn"); echo strtr("Hello world",$arr); ?>
Output:
Hello php.cn Hi php.cn
php strtr() function usage example 2:
<?php $arr = array("Hello" => "Hi", "world" => "php.cn"); echo strtr("Hello world",$arr); ?>
Output:
Hi php.cn
The above is the detailed content of How to use php strtr function. For more information, please follow other related articles on the PHP Chinese website!