Home > Backend Development > PHP Tutorial > Usage and difference between string replacement functions strtr() and str_repalce() in PHP

Usage and difference between string replacement functions strtr() and str_repalce() in PHP

墨辰丷
Release: 2023-03-28 13:42:01
Original
1894 people have browsed it

The replacement functions in PHP mainly include strtr() and str_repalce(). The following article mainly introduces the difference and usage between the two. The article introduces it in detail through example code. Friends in need can refer to it, and follow the editor to learn together.

First, let’s take a look at the two uses of this PHP string replacement function strtr():

strtr(string,from,to) Or strtr(string,array) First, focus on the first way of strtr function:

Let’s look at the following example:

##

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

The result is: I lOve yOu

This result reminds us:<br/>

1.strtr is case-sensitive <br/>

2.strtr replacement is very special, please pay attention to the yOu at the back and the O in the middle Replaced, this is obviously not our intention.

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, that is, the last parameter cannot be an empty string, of course, spaces are allowed. of.

Another example of another case of strtr function:

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

The result is: I lOvEs yOu

Note that A in the third parameter does not appear in the result.

<br/>

4. I do not recommend using strtr to exchange less for more. <br/>

ok, since this strtr function is quite troublesome, why do you still use it?

<br/>

The reason is that it is very fast. It is said that strtr is four times faster than str_replace.

<br/>

5. Be sure to use the strtr function when you can. <br/>

How to use it comfortably?

<br/>

This is its second case:

<br/>

strtr(string,array)
Copy after login

6.strtr Intentional use 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 replace whatever you want What to add to the array<br/>

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

Let me remind you again that writing Love as love will not work.

String replacement.

<br/>

Syntax:

string str_replace(string needle, string str, string haystack);<br/>

Return value: string

<br/>

Function type: Data processing

Content description:<br/>

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

<br/>

The following example replaces %body% with black

<br/>

<?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('Old1','Old2','Old3'), array('New1','New2','New3'), $The variable name of the replaced content )]


[@str_replace(array('old1','old2','old3'), 'new content', $Variable name of the replaced content)]

Example: Many-to-one replacement: I want to clear all

tags in the content field and replace them with Empty [

@str_replace(array('

','

'), '', $Content)

]One-to-one replacement: want to replace the content All
tags in the field are replaced with

[

@str_replace('
', '

', $Content)

]More Multiple replacement: I want to replace
in the content field with
, replace

with


, and clear all

[

@str_replace(array ('
', '

','

')

, array('<br />','<hr>','' ), $Content) ]The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP method to get audio file duration


##PHP Implementing the method of reading xml files


PHP method of restricting IP access_

php

Tips

The above is the detailed content of Usage and difference between string replacement functions strtr() and str_repalce() in PHP. 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