What are the methods of replacing the Str_replace () function with the PHP string? (Four types)

Source: Internet
Author: User
Tags mixed
What this article brings to you is about the method of replacing the Str_replace () function with the PHP string. (four kinds), there is a certain reference value, the need for friends can refer to, I hope to help you.

The following article to share the PHP string replacement str_replace () function 4 uses, the specific content is as follows:

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 $str 1 = str_replace ("Red", "Black", "Red green Yellow  Pink Purple ");  echo $str 1. "";  The 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); Print_r ($str 1);?  ><?php//Instance three: Array substitution array, map substitution $arr 1 = 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); Print_r ($con _rep);?  ><?php//Instance four: $search = Array ("Banana", "grape") if the array is $replace as a string $search;  $replace = "tomato";  $arr = Array ("Banana", "apple", "orange", "Grape");  $new _arr = Str_replace ($search, $replace, $arr, $count); Print_r ($new _arr);? 

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.