Replacement functions: 1. str_ireplace(); 2. str_replace(); 3. substr_replace(); 4. array_replace(); 5. array_replace_recursive(); 6. array_splice().
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php string search Replacement function
str_ireplace(): Replace some characters in the string (not case sensitive).
str_replace(): Replace some characters in the string (case-sensitive).
substr_replace(): Replace part of a string with another string.
str_ireplace() and str_replace() functions
str_ireplace() and str_replace use a new string to replace the specified string in the original string. For strings, str_replace is case-sensitive, str_ireplace() is not case-sensitive, and their syntax is similar.
The syntax of str_ireplace() is as follows:
mixed str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
This function returns a string or array. This string or array is the result of replacing all search in subject with replace (ignoring case). The parameter count represents the number of times to perform the replacement.
Usage examples are as follows:
<?php $str = 'hello,world,hello,world'; $replace = 'hi'; $search = 'hello'; echo str_ireplace($search, $replace, $str); ?>
The output result of executing the above code is:
hi,world,hi,world
substr_replace() function
substr_replace( ) The syntax of the function is as follows:
mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )
substr_replace() Replaces the substring qualified by the start and optional length parameters in a copy of string string using replacement.
If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.
If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.
The usage example of this function is as follows:
<?php $str = 'hello,world,hello,world'; $replace = 'hi'; echo substr_replace($str, $replace, 0,5); ?>
The execution result of the above code is:
hi,world,hello,world
php array search and replacement function
array_replace(): Replace the value of the first array with the value of the subsequent array.
array_replace_recursive(): Recursively replace the value of the first array with the value of the subsequent array.
array_splice(): Remove and replace the specified elements in the array.
array_splice() function
array_splice() function is used to delete part of the elements of the array; you can delete it directly or use other values. to replace.
array_splice() syntax is as follows:
array array_splice ( array &$arr, int $start [, int $length = 0 [, mixed $replacement ]] )
Parameter description:
If the result of the combination of start and length is that no elements will be deleted, then the value contained in replacement will be inserted into the position specified by start.
Note that using replacement to replace array elements will not retain the original key names.
Return value: Returns an array consisting of the deleted elements.
Examples of using the function are as follows:
<?php $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 2); print_r($arr); //$arr 现在变成 array("red", "green") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, -1); print_r($arr); //$arr 现在变成 array("red", "yellow") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, count($arr), "orange"); print_r($arr); //$arr 现在变成 array("red", "orange") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, -1, 1, array("black", "maroon")); print_r($arr); //$input 现在变成 array("red", "green", "blue", "black", "maroon") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 3, 0, "purple"); print_r($arr); //$arr 现在变成 array("red", "green", "blue", "purple", "yellow"); ?>
The output result of executing the above program is as follows:
Array ( [0] => red [1] => green ) Array ( [0] => red [1] => yellow ) Array ( [0] => red [1] => orange ) Array ( [0] => red [1] => green [2] => blue [3] => black [4] => maroon ) Array ( [0] => red [1] => green [2] => blue [3] => purple [4] => yellow )
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the find and replace functions in php. For more information, please follow other related articles on the PHP Chinese website!