Usage and difference of str_replace and str_ireplace in php

Source: Internet
Author: User
Usage and difference of str_replace and str_ireplace in php
Str_replace () function definition: replace other characters in a string with one string. it is a case-sensitive search syntax: str_replace (find, replace, string, count) str_ireplace () function definition: use a string to replace other characters in the string. the case-insensitive search syntax is str_ireplace (find, replace, string, count)


Str_replace
If no array is used, this function replaces all the search results with replace and returns the replaced string. For example, str_replace ("m", "n", "my name is jim! ") Ny nane is jin!

1. use arrays only for search.
Example: str_replace (array ('M', 'I'), 'n', "my name is jim! "); Return value: ny nane ns jnn!
It can be seen that each string in the array is replaced by the function sequence, and the replaced string is returned.

2. use arrays only for replace.
Example: str_replace ('M', array ('N', 'z'), "my name is jim! N ") return: Arrayy naArraye is jiArray!
This replacement is interesting. if you only use an Array for the second parameter, the function uses it as a string Array and replaces all search with an Array.

3. use arrays only for subject.

Example: str_replace ("m", "n", array ("my name is jim! "," The game is over! ") The execution result of this statement returns an array, that is, the result after the input two strings are replaced.
If the output array content is: ny nane is jin! The gane is over!

4. use arrays for search and replace.

Example: str_replace (array ("m", "I"), array ("n", "z"), "my name is jim! ") Return value: ny nane zs jzn!
Check the execution result. if the first two parameters use an array, the function replaces each object string of 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! It can be seen that the strings in the search array are replaced with empty strings.
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! The redundant replace items are ignored.

5. All three parameters use arrays.

Example: str_replace (array ("m", "I"), array ("n", "z"), array ("my name is jim! "," The game is over ") returned array content: ny nane zs jzn! The gane zs over
This is easy to understand. replace the two strings.


Example
  

Output:

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


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.