Summary of str_replace functions in php

Source: Internet
Author: User
The str_replace function is often used in actual development. for example, we often operate on strings. in this case, we will inevitably replace strings frequently. The str_replace function is often used in actual development. for example, we often operate on strings. here, we will inevitably replace strings frequently, mastering this function is of great help to php development.
This article describes how to use str_replace in PHP. The str_replace function is the preferred function for text replacement in PHP.
Applicable PHP versions: PHP 3> = 3.0.9, PHP 4, PHP 5

Str_replace syntax
Mixed str_replace (mixed search, mixed replace, mixed subject [, int & count])
Search: string to be replaced, replace: string to be replaced, subject: Operation string, count: number of replacements [optional parameter]

Use str_replace
You can run the following code and debug it.
// Provides:
$ Bodytag = str_replace ("% body %", "black ","");

// Provides: Hll Wrld f PHP
$ Vowels = array ("a", "e", "I", "o", "u", "A", "E", "I ", "O", "U ");
$ Onlyconsonants = str_replace ($ vowels, "", "Hello World of PHP ");

// Provides: You shoshould eat pizza, beer, and ice cream every day
$ Phrase = "You shoshould eat fruits, vegetables, and fiber every day .";
$ Healthy = array ("fruits", "vegetables", "fiber ");
$ Yummy = array ("pizza", "beer", "ice cream ");

$ Newphrase = str_replace ($ healthy, $ yummy, $ phrase );

// Use of the count parameter is available as of PHP 5.0.0
$ Str = str_replace ("ll", "", "good golly miss molly! ", $ Count );
Echo $ count; // 2
?>
Str_replace
If no array is used, this function replaces all the search results with replace and returns the replaced string. For example, str_replace ("m", "n", "my name is jim! ") Ny nane is jin!

1. use arrays only for search.
Example: str_replace (array ('M', 'I'), 'n', "my name is jim! "); Return value: ny nane ns jnn!
It can be seen that each string in the array is replaced by the function sequence, and the replaced string is returned.

2. use arrays only for replace.
Example: str_replace ('M', array ('N', 'z'), "my name is jim! \ N ") return: Arrayy naArraye is jiArray!
This replacement is interesting. if you only use an Array for the second parameter, the function uses it as a string Array and replaces all search with an Array.

3. use arrays only for subject.
Example: str_replace ("m", "n", array ("my name is jim! "," The game is over! ") The execution result of this statement returns an array, that is, the result after the input two strings are replaced.
If the output array content is: ny nane is jin! The gane is over!

4. use arrays for search and replace.
Example: str_replace (array ("m", "I"), array ("n", "z"), "my name is jim! ") Return value: ny nane zs jzn!
Check the execution result. if the first two parameters use an array, the function replaces each object string of the array, and the first item of search is replaced with the first item of replace. And so on.
If the search array is longer than new_deedle, for example, str_replace (array ("m", "I", "s"), array ("n", "z "), "my name is jim! "); Return: ny nane z jzn! It can be seen that the strings in the search array are replaced with empty strings.
If the replace array is longer than search, for example, str_replace (array ("m", "I"), array ("n", "z", "x "), "my name is jim! ") Returns ny nane zs jzn! The redundant replace items are ignored.

5. All three parameters use arrays.
Example: str_replace (array ("m", "I"), array ("n", "z"), array ("my name is jim! "," The game is over ") returned array content: ny nane zs jzn! The gane zs over
This is easy to understand. replace the two strings. The str_replace function is often used in actual development. for example, we often operate on strings. here, we will inevitably replace strings frequently, mastering this function is of great help to php development.
This article describes how to use str_replace in PHP. The str_replace function is the preferred function for text replacement in PHP.
Applicable PHP versions: PHP 3> = 3.0.9, PHP 4, PHP 5

Str_replace syntax
Mixed str_replace (mixed search, mixed replace, mixed subject [, int & count])
Search: string to be replaced, replace: string to be replaced, subject: Operation string, count: number of replacements [optional parameter]

Use str_replace
You can run the following code and debug it.
// Provides:
$ Bodytag = str_replace ("% body %", "black ","");

// Provides: Hll Wrld f PHP
$ Vowels = array ("a", "e", "I", "o", "u", "A", "E", "I ", "O", "U ");
$ Onlyconsonants = str_replace ($ vowels, "", "Hello World of PHP ");

// Provides: You shoshould eat pizza, beer, and ice cream every day
$ Phrase = "You shoshould eat fruits, vegetables, and fiber every day .";
$ Healthy = array ("fruits", "vegetables", "fiber ");
$ Yummy = array ("pizza", "beer", "ice cream ");

$ Newphrase = str_replace ($ healthy, $ yummy, $ phrase );

// Use of the count parameter is available as of PHP 5.0.0
$ Str = str_replace ("ll", "", "good golly miss molly! ", $ Count );
Echo $ count; // 2
?>
Str_replace
If no array is used, this function replaces all the search results with replace and returns the replaced string. For example, str_replace ("m", "n", "my name is jim! ") Ny nane is jin!

1. use arrays only for search.
Example: str_replace (array ('M', 'I'), 'n', "my name is jim! "); Return value: ny nane ns jnn!
It can be seen that each string in the array is replaced by the function sequence, and the replaced string is returned.

2. use arrays only for replace.
Example: str_replace ('M', array ('N', 'z'), "my name is jim! \ N ") return: Arrayy naArraye is jiArray!
This replacement is interesting. if you only use an Array for the second parameter, the function uses it as a string Array and replaces all search with an Array.

3. use arrays only for subject.
Example: str_replace ("m", "n", array ("my name is jim! "," The game is over! ") The execution result of this statement returns an array, that is, the result after the input two strings are replaced.
If the output array content is: ny nane is jin! The gane is over!

4. use arrays for search and replace.
Example: str_replace (array ("m", "I"), array ("n", "z"), "my name is jim! ") Return value: ny nane zs jzn!
Check the execution result. if the first two parameters use an array, the function replaces each object string of the array, and the first item of search is replaced with the first item of replace. And so on.
If the search array is longer than new_deedle, for example, str_replace (array ("m", "I", "s"), array ("n", "z "), "my name is jim! "); Return: ny nane z jzn! It can be seen that the strings in the search array are replaced with empty strings.
If the replace array is longer than search, for example, str_replace (array ("m", "I"), array ("n", "z", "x "), "my name is jim! ") Returns ny nane zs jzn! The redundant replace items are ignored.

5. All three parameters use arrays.
Example: str_replace (array ("m", "I"), array ("n", "z"), array ("my name is jim! "," The game is over ") returned array content: ny nane zs jzn! The gane zs over
This is easy to understand. replace the two strings.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.