Php string replacement (str_replace)

Source: Internet
Author: User
Tags php tutorial

Another case of strtr functions

<? Php Tutorial echo strtr ("I loves you", "love", "lovea");?>
The result is

I loves you

Note that a of the third parameter does not appear in the result.

4. I do not recommend replacing strtr with less

Okay. Why should I use this strtr function?
The reason is that it is fast
It is said that strtr is four times faster than str_replace

5. Use the strtr function to replace php strings.

How can it be used for comfort?
This is the second case.
Strtr (string, array)

6. Use strtr as expected

<? Php $ table_change = array ('you' => 'Her sister'); echo strtr ("I love you", $ table_change);?>
I love her sister

7. Tips: If you want to replace the php string with the replacement string, add the replacement string to the array.

For example

<? Php $ table_change = array ('you' => 'Her sister'); $ table_change + = array ('Love' => 'hate '); echo strtr ("I love you", $ table_change);?>
The result is

I hate her sister




Replacement is to change a part of a string to a new character string to meet new requirements. In php, use str_replace ("content to be replaced", "string to replace the original content", and "original string") to replace.
Echo str_replace ("iwind", "kiki", "I love iwind, iwind said"); // output "I love kiki, kiki said"
Replace all "iwind" in the original string with "kiki ".

Str_replace is case sensitive, so you cannot imagine replacing the "iwind" in the original string with str_replace ("iwind", "kiki ".

Str_replace can also replace multiple-to-one and multiple-to-many, but cannot replace one-to-multiple:
Echo str_replace (array ("iwind", "kiki"), "people", "I love kiki, iwind said ");
Will output
I love people, people said
Array ("iwind", "kiki") in the first parameter is replaced with "people"

Echo str_replace (array ("iwind", "kiki"), array ("gentle man", "ladies"), "I love kiki, iwind said ");

Output I love ladies and gentle man said. That is to say, the elements in the first array are replaced by the corresponding elements in the second array. If there is an array with fewer elements than the other array, the insufficiency will be treated as null.

Similar to strtr,

In addition, php also provides substr_replace to replace some strings. Syntax:

Substr_replace (original string, string to be replaced, starting position [, length of replacement])

Here, the starting position of replacement starts from 0 and should be smaller than the length of the original string. The length to be replaced is optional.

Echo substr_replace ("abcdefgh", "def", 3); // output "abcdef"
Echo substr_replace ("abcdefgh", "def", 3, 2); // output "abcdeffgh"

In the first example, replace "defgh" with "def" starting from the third position (that is, "d"

In the second example, it is also replaced from the third position (that is, "d"), but only two lengths can be replaced, that is, to e, therefore, replace "de" with "def ".

Php also provides functions such as preg_replace, preg_replace_callback, ereg_replace, and eregi_replace to use regular expressions to replace strings. For usage instructions, see the manual.

Definition and usage
The str_replace () function uses one string to replace other characters in the string.

Str_replace (find, replace, string, count) parameter description
Find is required. Specifies the value to be searched.
Replace is required. Specifies the value to replace the value in find.
String is required. Specifies the string to be searched.
Count is optional. A variable that counts the number of replicas.
Tips and comments
Note: This function is case sensitive. Use str_ireplace () to perform a case-insensitive search.

Note: This function is binary secure.
Example 1
<? Php
Echo str_replace ("world", "john", "hello world! ");

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.