Home  >  Article  >  Backend Development  >  How to replace specified string in php?

How to replace specified string in php?

coldplay.xixi
coldplay.xixiOriginal
2020-07-11 13:35:4412467browse

php method to replace the specified string: 1. [strtr] is to convert the specified character, the code is [string strtr(string $str,$replace_pairs)]; 2. [str_replace()] function uses other Characters replace some characters in a string.

How to replace specified string in php?

php method to replace the specified string:

In PHP, there are two functions that can implement characters String replacement, strtr() and str_repalce() functions.

1. First, 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, &#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>";
?>

Access through the browser, the result is as follows:

How to replace specified string in php?

You can combine the code and return value to understand the rules for replacing strings with the syntax of the strtr function.

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 "....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 two:

<?php
$str = "PHP中文网";
echo strtr($str, array("中" => &#39;&#39;)) . &#39;<br>&#39;;
echo strtr($str, array("中" => &#39;.&#39;, "文网" => "cn")) . &#39;<br>&#39;;
echo "<hr>";
?>

Access through the browser, the result is as follows:

How to replace specified string in php?

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 replacing specified strings in PHP. Using the strtr function can not only replace multiple strings but also replace Chinese strings, etc.

2. str_replace()The function replaces some characters in the string with other characters (case sensitive).

This function must follow the following rules:

  • If the searched string is an array, then it will return an array.

  • If the searched string is an array, then it will find and replace each element in the array.

  • If you need to search and replace the array at the same time, and the elements that need to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings

  • If the search is an array and the replacement is a string, then the replacement string will work on all found values.

Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.

Note: This function is binary safe.

<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "替换数:$i";
?>

How to replace specified string in php?

<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>

How to replace specified string in php?

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to replace specified string in php?. 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