This time watching "PHP and MySQL Web development," a book to see Str_replace, a little hint to write: You can use the array for the Str_replace three, but the explanation is relatively simple, so decided to test their own the function of the parameters passed in the array 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: Alternate number of "Optional parameters"
we focus on testing the top three in the use of arrays is the way to execute:
when neither array is used, the function replaces all needle directly 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! '); Back: NY nane NS jnn!
can see that the function order is substituted for each string in the array and returns the replacement string.
2, use arrays only for new_needle.
Example: Str_replace (' m ', array (' n ', ' Z '), "My name is jim!\n") returns: Arrayy Naarraye is jiarray!
This substitution is interesting, if you use an array for only the second parameter, the function uses it as a string array, replacing all 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 execution result of the statement returns an array, which is the result of the replacement of the two strings that were passed in.
If you see the output array contents: NY nane is jin! The Gane is over!
4, using arrays for both needle and new_needle.
Example: Str_replace (Array ("M", "I"), Array ("n", "Z"), "My name is jim!") Back: NY nane ZS jzn!
viewing execution results show that if the first two parameters are used in an array, the function replaces each object item string in the array, and the first item of needle is replaced by the first item of the New_needle. Analogy
If the needle array is longer than New_deedle, for example: Str_replace (Array ("M", "I", "s"), Array ("n", "Z"), "My name is jim!"); Back: NY nane z jzn! Visible, strings that are more than needle arrays are replaced with 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!") Return to NY nane ZS jzn! visible new_needle superfluous items are ignored.
5, three parameters all use an array.
For example: Str_replace (Array ("M", "I"), Array ("n", "Z"), Arrays ("My name is jim!", "The game are Over"), returns the contents of the element: NY nane ZS Jzn!the Gane Zs over
This is a good idea to perform a replacement for two strings individually.
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.