Home > Daily Programming > PHP Knowledge > How to replace a specified string in PHP? (Pictures + Videos)

How to replace a specified string in PHP? (Pictures + Videos)

藏色散人
Release: 2019-11-08 17:57:29
Original
7214 people have browsed it

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

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

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, &#39;P&#39;, &#39;A&#39;) . &#39;<br>&#39;;
echo strtr($str, &#39;PP&#39;, &#39;z1&#39;) . &#39;<br>&#39;;
echo strtr($str, &#39;P&#39;, &#39; &#39;) . &#39;<br>&#39;;
echo strtr($str, &#39;PH&#39;, &#39;12&#39;) . &#39;<br>&#39;;
echo "<hr>";
?>
Copy after login

Access through the browser, the result is as follows:

How to replace a specified string in PHP? (Pictures + Videos)

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("中" => &#39;&#39;)) . &#39;<br>&#39;;
echo strtr($str, array("中" => &#39;.&#39;, "文网" => "cn")) . &#39;<br>&#39;;
echo "<hr>";
?>
Copy after login

Access through the browser, the result is as follows:

How to replace a specified string in PHP? (Pictures + Videos)

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!

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