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

黄舟
Release: 2023-03-17 07:06:02
Original
1793 people have browsed it

Example

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

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

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)
Copy after login

or

strtr(string,array)
Copy after login
ParametersDescription
stringRequired. Specifies the string to be converted.
fromRequired (unless array is used). Specifies the character to be changed.
toRequired (unless using an array). Specifies the character to change to.
arrayRequired (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);
?>
Copy after login

Example

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

Output:

Hello World example 2

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

Output:


Hi earth
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template