Replace function Summary of str_replace function usage in PHP

Source: Internet
Author: User
Tags function prototype php and mysql
This time in reading "PHP and MySQL Web development" a book to see Str_replace explanation, a little hint to write: can be used in the array of Str_replace three, but the explanation is relatively simple, so decided to experiment with the function in the parameters of each parameter when the execution results.
Function prototype: Mixed str_replace (mixed needle,mixed new_needle,mixed haystack[,int &count]);
Needle: String to be replaced, New_needle: replacement string, Haystack: action string, Count: substitution "optional parameter"
We focused on testing the top three in using arrays is the way of execution:
When you do not use an array, the function directly replaces all needle with new_needle and returns the replaced string. such as: Str_replace ("M", "N", "My Name is jim!") Back to NY nane is jin!
1. Use arrays only for needle.
Example: Str_replace (Array (' m ', ' I '), ' n ', "My name is jim!"); return: NY nane NS jnn!
As you can see, the function is sequentially substituted for each string in the array and returns the replaced string.
2. Use arrays only for new_needle.
Example: Str_replace (' m ', array (' n ', ' Z '), "My name is jim!\n") returned: Arrayy Naarraye is jiarray!
This substitution is interesting if you use the array for the second argument only, and the function uses it as a string array, replacing all the needle with the array.
3. Use arrays only for haystack.
Example: Str_replace ("M", "N", Array ("My name is jim!", "The game is over!")) The result of the statement execution returns an array, which is the result of replacing the two strings that were passed in.
If the output array contents will be seen: NY nane is jin! The Gane is over!
4. Use arrays for needle and new_needle.
Example: Str_replace (Array ("M", "I"), Array ("n", "Z"), "My name is jim!") Back to: NY nane ZS jzn!
Looking at the execution results, you can see that if the first two parameters are all using an array then the function replaces each object item string in the array, and the first item of needle is replaced with the first item of 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! Visible, strings that are more than the needle array are replaced for empty strings.
If the New_needle array is longer than needle, for example: Str_replace (Array ("M", "I"), Array ("n", "Z", "X"), "My name is jim!") Returns NY nane ZS jzn! Visible new_needle superfluous entries are ignored.
5, three parameters all use arrays.
For example: Str_replace (Array ("M", "I"), Array ("n", "Z"), Array ("My name is jim!", "The game was over")) returns the contents of the arrays: NY nane ZS jzn!the Gane ZS Over
This is a good understanding, performing a replacement on two strings.
Simply summarize and prevent yourself from forgetting!

The above describes the Replace function PHP Str_replace function Use summary, including the replace function aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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