PHP str_ireplace () Str_replace usage and other areas _php tutorials

Source: Internet
Author: User
The Str_ireplace () str_replace function in PHP is the substitution of characters, and I think the biggest difference is that one is not sensitive to case, and the other is the same for case sensitivity.

Str_replace () function

Definition: Use a string to replace some other characters in a string, a case-sensitive search
Grammar:

Str_replace (Find,replace,string,count)
Instance:

The code is as follows Copy Code

Output

Hupengstudying.com Hupeng

Through the above description and example comparison, I believe that everyone is very clear understanding of PHP str_replace () function and Str_ireplace () function of the difference

Str_replace use
You can actually run the following code, debugging.


$bodytag = Str_replace ("%body%", "Black", "

Provides:hll Wrld F PHP
$vowels = Array ("A", "E", "I", "O", "U", "a", "E", "I", "O", "U");
$onlyconsonants = Str_replace ($vowels, "", "Hello World of PHP");

Provides:you should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = Array ("Fruits", "vegetables", "fiber");
$yummy = Array ("Pizza", "beer", "ice Cream");

$newphrase = Str_replace ($healthy, $yummy, $phrase);

Use of the Count parameter is available as of PHP 5.0.0
$str = Str_replace ("ll", "" "," Good Golly Miss Molly! ", $count);
Echo $count; 2
?>

");
The code is as follows Copy Code

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.


Analysis on the cause of garbled str_replace


Garbled problem is caused by Chinese character coding and str_replace work party, because a Chinese character is accounted for two bytes, full-width space is equivalent to the same as Chinese characters accounted for two bytes, a region code, a bit code. However, the bit code and area code have overlapping parts. such as the following code:

The code is as follows Copy Code

/*
Xa1xa1 = "" (Chinese full-width space)
Xcdxa1 = "ting"
Xa1xa3 = ". "(Chinese full-width period)
Xcdxa3 = "Stop"
*/

$str = "ting. ";
echo Str_replace ("", "", $str);
?>;

The output of the above code is the "Stop" word. Because Str_replace is a byte-by-byte comparison, it makes an error


http://www.bkjia.com/PHPjc/445620.html www.bkjia.com true http://www.bkjia.com/PHPjc/445620.html techarticle the Str_ireplace () str_replace function in PHP is the substitution of characters, and I think the biggest difference is that one is not sensitive to case, and the other is the same for case sensitivity. Str_rep ...

  • Related Article

    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.