Usage and differences of str_replace and Str_ireplace in PHP

Source: Internet
Author: User

Str_replace () function definition: Use a string to replace some other characters in a string, the case-sensitive search syntax: Str_replace (Find,replace,string,count) str_ireplace () function definition: Use a string to replace some other characters in a string with the case insensitive search syntax: Str_ireplace (Find,replace,string,count)


Str_replace detailed
When you do not use an array, the function uses replace directly to replace all search and return the replaced string. such as: Str_replace ("M", "N", "My Name is jim!") Back to NY nane is jin!

1. Use arrays only for search.
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 replace.
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 search with the array.

3. Use arrays only for subject.

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 both search and replace.

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 search is replaced with the first item of replace. And so on
If the search 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, the string that is extra for the search array is replaced with an empty string.
If the replace array is longer than search, for example: Str_replace (Array ("M", "I"), Array ("n", "Z", "X"), "My name is jim!") Returns NY nane ZS jzn! Visible Replace 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.


Example
<?php$find = Array ("Hello", "World"), $replace = Array ("B"), $arr = Array ("Hello", "World", "!"); Print_r (Str_ireplace ($find, $replace, $arr));? >

Output:

Array ([0] = b[1] =>[2] = =!)



Usage and differences of str_replace and Str_ireplace in PHP

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.