Home  >  Article  >  Backend Development  >  What is the php replacement function?

What is the php replacement function?

王林
王林Original
2019-09-19 11:54:112751browse

What is the php replacement function?

The replacement functions in php mainly include strtr() and str_repalce(). Today we will introduce their differences and usage. , let’s first take a look at the two uses of this PHP string replacement function strtr():

strtr(string,from,to)orstrtr (string,array)First of all, focus on the first method of strtr function:

Let’s take a look at the following example:

<?php
echo strtr("I Love you","Lo","lO");
?>

The result obtained is: I lOve yOu

This result reminds us:

1. strtr is case-sensitive

2. The replacement of strtr is very special

Please look at the yOu at the back. The O in the middle has been replaced. This is obviously not our intention. Let me give another special example to illustrate the weirdness of this PHP sttr function.

<?php
echo strtr("I Love you","Love","");
?>

The result is: I Love you

Nothing will change, so what strtr needs to pay attention to is:

3. It cannot be replaced by empty, nor That is, the last parameter cannot be an empty string. Of course, spaces are allowed.

Another example of another case of the strtr function:

<?php
echo strtr("I Loves you","Love","lOvEA");
?>

The result is: I lOvEs yOu

Pay attention to the A in the third parameter, in the result did not appear.

4. I don’t recommend using strtr to exchange less for more

ok. Since this strtr function is quite troublesome, why use it? The reason is, it's fast. It is said that strtr is four times faster than str_replace.

5. When you can use the strtr function, you must use it

The second case:

strtr(string,array)

6. strtr meets your wishes Usage method

<?php
$table_change = array(&#39;you&#39;=>&#39;her sister&#39;);
echo strtr("I Love you",$table_change);
?>

The result is: I Love her sister

7. Tips: Just add whatever you want to replace to the array

For example:

<?php
$table_change = array(&#39;you&#39;=>&#39;her sister&#39;);
$table_change += array(&#39;Love&#39; => &#39;hate&#39;);
echo strtr("I Love you",$table_change);
?>

The result is: I hate her sister

Remind me again that Love written as love will not work.

String replacement

Syntax:

string str_replace(string needle, string str, string haystack);

Return value: String

Function type: Data processing

Content description:

This function substitutes the string str into the haystack string and replaces all needles with str.

The following example replaces %body% with black

<?php
$bodytag = str_replace("%body%", "black", "<body text=%body%>");
echo $bodytag;
?>

Format:

[@str_replace("Old content to be replaced" , "New characters to replace the original content", $Variable name of the replaced content)] [@str_replace(array('Old 1','Old 2','Old 3'), array('New 1',' New 2', 'New 3'), $Variable name of the replaced content)] [@str_replace(array('Old 1', 'Old 2', 'Old 3'), 'New content', $Replaced content Variable name)]

Example:

Many-to-one replacement: I want alle388a4556c0f65e1904146cc1a846bee5d37557a868095f7d6a1a4be959c4fed tag is cleared and replaced with empty [@str_replace(array('e388a4556c0f65e1904146cc1a846bee','94b3e26ee717c64999d7867364b1b4a3'), '', $Content)]
One-to-one replacement: I want to replace all 0c6dc11e160d3b678d68754cc175188a tags in the content field with e388a4556c0f65e1904146cc1a846bee [@str_replace('0c6dc11e160d3b678d68754cc175188a', 'e388a4556c0f65e1904146cc1a846bee', $Content)]
Multiple pairs Multi-replace: I want to replace 0c6dc11e160d3b678d68754cc175188a in the content field with df250b2156c434f3390392d09b1c9563, replace e388a4556c0f65e1904146cc1a846bee with f32b48428a809b51f04d3228cdf461fa, and clear all 94b3e26ee717c64999d7867364b1b4a3[@str_replace(array( '0c6dc11e160d3b678d68754cc175188a', 'e388a4556c0f65e1904146cc1a846bee','94b3e26ee717c64999d7867364b1b4a3'), array('df250b2156c434f3390392d09b1c9563','f32b48428a809b51f04d3228cdf461fa',''), $Content)].

The above content is for reference only!

Recommended tutorial: PHP video tutorial

The above is the detailed content of What is the php replacement function?. 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