PHP string Substitution str_replace () function 4 usage explanations

Source: Internet
Author: User

mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])
the function returns a string or an array. The string or array is the result of replacing all search in subject with replace.
1, $search, the string to replace, or the array
2. $replace, the string or array to replace
3. $subject, the string or array being queried
4, $count, optional, if specified, will be set to replace the number of times
5. Return value: The function returns the replaced array or string (reborn)

<?php//instance One: string substitution string $str1 = Str_replace ("Red", "Black", "Red green yellow Pink Purple"), echo $str 1. ""; /output is black green yellow pink purple?>
<?php//instance Two: String substitution array key value $arr = Array ("Blue", "Red", "green", "yellow"), $str 1 = str_replace ("Red", "pink", $arr, $i);p rint _r ($str 1);? >
<?php//instance three: Array substitution array, map substitution $arr1 = Array ("Banana", "orange"), $arr 2 = Array ("Pitaya", "tomato"), $con _arr = Array ("Apple", "Orange", "banana", "grape"), $con _rep = Str_replace ($arr 1, $arr 2, $con _arr, $count);p Rint_r ($con _rep);? >
<?php//instance four: If the $search is an array, the $replace is a string $search = Array ("Banana", "grape"), $replace = "tomato"; $arr = Array ("Banana", " Apple "," orange "," Grape "), $new _arr = Str_replace ($search, $replace, $arr, $count);p Rint_r ($new _arr);? >


This article from the "Golden Three" blog, declined to reprint!

PHP string Substitution str_replace () function 4 usage explanations

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.