Detailed description of the specific functions of the PHP function str_replace _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Detailed description of the str_replace function of PHP. During the actual period, we read the PHP function str_replace in the book PHP and MySQLWeb development. a small prompt says: we can use arrays for all three of str_replace.During this time, I read the book "PHP and MySQL Web development" and saw the PHP function str_replace explanation. I wrote a small note: Three of str_replace can be passed in using arrays, but the explanation is relatively simple, so I decided to test the execution result of the function when each parameter is passed into the array.

Function prototype: mixed str_replace (mixed needle, mixed new_needle, mixed haystack [, int & count]);
Needle: string to be replaced, new_needle: string to be replaced, haystack: Operation string, count: number of replacements (optional]

We focus on the execution method of the first three items using arrays:
When no array is used, the PHP function str_replace replaces all needle directly with new_needle and returns the replaced string. For example, str_replace ("m", "n", "my name is jim! ") Ny nane is jin!

1. use arrays only for needle.

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 new_needle.

Example of the str_replace function in PHP: 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 needle with an Array.

3. use arrays only for haystack.

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 both needle and new_needle.

Example: str_replace (array ("m", "I"), array ("n", "z"), "my name is jim! ") Return value: ny nane zs jzn!
View the execution result. if the first two parameters use an array, the function replaces each object string in the array and the first item in needle with the first item in new_needle. And so on.
If the needle 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 that come out of the needle array are replaced with empty strings.
If the new_needle array is longer than the needle, for example, str_replace (array ("m", "I"), array ("n", "z", "x "), "my name is jim! ") Returns ny nane zs jzn! The excess new_needle items are ignored.

5. All three parameters use arrays.

Example of the str_replace function in PHP: 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.


During this time, I saw the PHP function str_replace in the book PHP and MySQL Web development. I wrote a small note: Three of str_replace can be passed in using arrays ,...

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.