Home >Backend Development >PHP Tutorial >PHP function strtr() that converts specific characters in a string

PHP function strtr() that converts specific characters in a string

黄舟
黄舟Original
2017-11-06 10:45:351846browse

Example

Replace the character "ia" in string with "eo":

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

Definition and usage

strtr() FunctionConvert specific characters in a string.

Note: If the lengths of the from and to parameters are different, format them to the shortest length.

Syntax

strtr(string,from,to)

or

strtr(string,array)
Parameters Description
string Required. Specifies the string to be converted.
from Required (unless array is used). 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 in which the key name is the original character and the key value is the target character.

Technical details

Return value: Returns the converted characters.
If the array parameter contains an empty string ("") key name, return FALSE.
PHP version: 4+
##More examples

Example 1

Replace the string "Hello world" with "Hi earth":

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

Example

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

Output:

Hello World example 2

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

Output:


Hi earth

The above is the detailed content of PHP function strtr() that converts specific characters in a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn