Home >Daily Programming >PHP Knowledge >How to replace a specified string in PHP? (Pictures + Videos)
PHP replaces the specified string , which is usually asked as a basic knowledge point during the PHP interview process. Then in PHP, there are two functions that can implement string replacement, strtr() and str_repalce() functions.
First of all, let’s briefly understand the definition and syntax of the strtr() function.
strtr: Convert specified characters.
Two syntaxes:
The first syntax:
string strtr( string $str, string $from, string $to)
The first parameter represents the string to be converted . The second parameter represents the source character in the string corresponding to the destination character to to be converted. The third parameter represents the destination character in the string corresponding to the character from to be converted.
Second syntax:
string strtr( string $str , array $replace_pairs )
The first parameter represents the string to be converted. The second parameter represents replacing the specified string in the form of an array key value.
Below we will combine two simple code examples to introduce in detail the usage and usage techniques of the most commonly used strtr() function.
Strtr function in PHP replaces string example 1:
<?php $str = "PHP中文网"; echo strtr($str, 'P', 'A') . '<br>'; echo strtr($str, 'PP', 'z1') . '<br>'; echo strtr($str, 'P', ' ') . '<br>'; echo strtr($str, 'PH', '12') . '<br>'; echo "<hr>"; ?>
Access through the browser, the result is as follows:
You can combine the code and return value to understand the rules for replacing strings with the strtr function syntax.
Some friends here may be a little confused about the result of replacing "z1" with "PP" in the second line. This is because if you want to convert two identical destination characters, the value of the last character is used as the standard. . Since the last "P" here corresponds to "1", the replacement result is converted from "PHP Chinese Network" to "1H1 Chinese Network".
The third line shows the result of replacing the space character, which is also very easy to understand. In other cases, just replace the corresponding characters.
Strtr function in PHP replaces string example 2:
<?php $str = "PHP中文网"; echo strtr($str, array("中" => '')) . '<br>'; echo strtr($str, array("中" => '.', "文网" => "cn")) . '<br>'; echo "<hr>"; ?>
Access through the browser, the result is as follows:
As shown in the figure, Chinese string replacement can also be achieved using the strtr function in PHP.
This article is about the specific method of PHP replacing the specified string. Using the strtr function can not only replace multiple strings but also replace Chinese strings, etc.
If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!
The above is the detailed content of How to replace a specified string in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!