What is the php replacement function?

王林
Release: 2023-02-24 12:48:02
Original
2814 people have browsed it

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

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

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

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

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

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

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

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

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 all

tag is cleared and replaced with empty [@str_replace(array('

','

'), '', $Content)]
One-to-one replacement: I want to replace all
tags in the content field with

[@str_replace('
', '

', $Content)]
Multiple pairs Multi-replace: I want to replace
in the content field with
, replace

with


, and clear all

[@str_replace(array( '
', '

','

'), array('
','
',''), $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!

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