How to use php strtr function?

青灯夜游
Release: 2023-02-23 06:00:01
Original
2760 people have browsed it

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

or

strtr(string,array)
Copy after login

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

Output:

Hello World
Copy after login

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

<?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 How to use php strtr function?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!