How to use php strtr function

藏色散人
Release: 2023-04-05 06:28:01
Original
3150 people have browsed it

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...

How to use php strtr function

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

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

Output:

Hello php.cn
Hi php.cn
Copy after login

php strtr() function usage example 2:

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

Output:

Hi php.cn
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