Home  >  Article  >  Backend Development  >  How to use php strtr function?

How to use php strtr function?

青灯夜游
青灯夜游Original
2019-05-30 13:50:452740browse

strtr() function is a built-in function in PHP that is used to replace a substring in a string with a given string. This function returns the converted string; if the lengths of the from and to parameters are different, they will be formatted to the shortest length; if the array parameter contains the key name of an empty string, it returns FALSE.

How to use php strtr function?

php How to use strtr() function?

strtr() function converts specific characters (or substrings) in a string.

Syntax:

strtr(string,from,to)

or

strtr(string,array)

Parameters:

● string: required. Specifies the string to be converted.

●from: required (unless using an array). Specifies the character (or substring) to be changed.

● to: required (unless using an array). Specifies the character (or string) to be changed 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.

Return value: Returns the converted string. If the lengths of the from and to parameters are different, they will be formatted to the shortest length; if the array parameter contains an empty string ("") key name, FALSE will be returned.

Let’s take a look at how to use the php strtr() function through an example.

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

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

Output:

Hello World

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

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

Output:

Hi earth

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!

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