Describes four usage of the PHP string replacement str_replace () function: str_replace

Source: Internet
Author: User

Describes four usage of the PHP string replacement str_replace () function: str_replace

The following describes how to replace str_replace () with a PHP string:

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

This function returns a string or array. This string or array is the result after all the searches in the subject are replaced by replace.

1. $ search, the string to be replaced, or an array

2. $ replace, a string or array to be replaced

3. $ subject: the queried string or Array

4. $ count (Optional). If specified, it is set to the number of replicas.

5. Return Value: This function returns the replaced array or string (new)

<? Php // instance 1: String replacement string $ str1 = str_replace ("red", "black", "red green yellow pink purple"); echo $ str1 .""; // The output result is black green yellow pink purple?> <? Php // Example 2: String replacement array key value $ arr = array ("blue", "red", "green", "yellow "); $ str1 = str_replace ("red", "pink", $ arr, $ I); print_r ($ str1);?> <? Php // Example 3: array replacement array, ing replacement $ arr1 = array ("banana", "orange"); $ arr2 = array ("pitaya", "tomato "); $ con_arr = array ("apple", "orange", "banana", "grape"); $ con_rep = str_replace ($ arr1, $ arr2, $ con_arr, $ count ); print_r ($ con_rep);?> <? Php // Example 4: If $ search is an array and $ replace is a string, $ search = array ("banana", "grape"); $ replace = "tomato "; $ arr = array ("banana", "apple", "orange", "grape"); $ new_arr = str_replace ($ search, $ replace, $ arr, $ count ); print_r ($ new_arr);?>

Summary

The above section describes four usage of the PHP string replacement str_replace () function. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.