PHP function Str_replace specific functions _php tutorial

Source: Internet
Author: User
We are in the actualThis time in reading "PHP and MySQL Web development" a book to see PHP function str_replace explanation, a little hint to write: You can use the array for the Str_replace three, but the explanation is relatively simple, and decided to experiment with it. The result of the function's execution when each parameter passes into an array.

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 arrays, the PHP function str_replace 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.

PHP function Str_replace Example: Str_replace (' m ', array (' n ', ' Z '), "My name is Jim!n") return: 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.

PHP function Str_replace 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.


http://www.bkjia.com/PHPjc/446318.html www.bkjia.com true http://www.bkjia.com/PHPjc/446318.html techarticle In the actual period of time in the read PHP and MySQL Web development in the book to see PHP function str_replace explanation, a small hint to write: can be used for the str_replace of the three of the array passed in, ...

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