Php str_replace () function

Source: Internet
Author: User
Definition and Usage
Definition and usage

The str_replace () function replaces some characters with some other characters in a string.
The str_replace () function is used to replace a substring with another string (Case Insensitive ).

This function works by the following rules:
This function must follow the following principles:

  • If the string to be searched is an array, it returns an array
    If the searched string is an array, it returns an array.
  • If the string to be searched is an array, find and replace is saved Med with every array element
    If the string to be searched is an array, it searches for and replaces each element in all arrays.
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
    If you need to search for and replace an array at the same time, and the number of elements to be replaced is less than the number of elements found, the extra elements will be replaced with a null string.
  • If find is an array and replace is a string, the replace string will be used for every find value
    If an array is searched, but only one string is replaced, the replacement string takes effect for all the searched values.
Syntax
Syntax
str_replace(find,replace,string,count)

Parameter Parameters Description
Find Required. Specifies the value to find
Required parameter. Specify the value to be searched
Replace Required. Specifies the value to replace the value inFind
Required parameter. Specify substitution value
String Required. Specifies the string to be searched
Required parameter. String to be searched
Count Optional. A variable that counts the number of replacements
Optional. Number of replicas to be replaced

Tips and Notes
Notes

Note:This function is case-sensitive. Use str_ireplace () to perform a case-insensitive search.
Note: The str_replace () function is case sensitive. If case sensitivity is not required, use the str_irreplace () function because it is case insensitive.

Note:This function is binary-safe.
Note: This function is "binary-accurate [binary-safe]".

Example 1
Case 1 <? Php
Echo str_replace ("world", "Peter", "Hello world! ");
?>

 

 

The output of the code above will be:
The above code will output the following results:

Hello Peter!

 

Example 2
Case 2

In this example we will demonstrate str_replace () with an array and a count variable:
In the following example, we use an array and a count variable to demonstrate the str_ireplace () function:

<? Php
$ Arr = array ("blue", "red", "green", "yellow ");
Print_r (str_replace ("red", "pink", $ arr, $ I ));
Echo "Replacements: $ I ";
?>

 

 

The output of the code above will be:
The above code will output the following results:

Array ([0] => blue [1] => pink [2] => green [3] => yellow) Replacements: 1

 

Example 3
Case 3

In this example we will demonstrate str_replace () with less elements in replace than find:
In the following example, we demonstrate how to use the str_replace () function to specify an alternative element that is less than the searched element:

 

<? Php
$ Find = array ("Hello", "world ");
$ Replace = array ("B ");
$ Arr = array ("Hello", "world ","! ");
Print_r (str_replace ($ find, $ replace, $ arr ));
?>
 

 

 

The output of the code above will be:
The above code will output the following results:

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.