Function of PHP function str_replace in string substitution _php tutorial

Source: Internet
Author: User
Tags php and mysql
ForThis 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. php function Str_replace only use arrays 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. php function Str_replace only use arrays 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. php function Str_replace only use arrays 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. php function Str_replace use arrays for both 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, PHP function str_replace Three parameters are used array.

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.


http://www.bkjia.com/PHPjc/446306.html www.bkjia.com true http://www.bkjia.com/PHPjc/446306.html techarticle for this time in read the "PHP and MySQL Web development" a book to see Str_replace explanation, a little hint wrote: Can be used for the str_replace of the three arrays to pass in, but the explanation is relatively simple ...

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