Home > Backend Development > PHP Problem > What are the find and replace functions in php

What are the find and replace functions in php

青灯夜游
Release: 2023-03-11 18:08:02
Original
3101 people have browsed it

Replacement functions: 1. str_ireplace(); 2. str_replace(); 3. substr_replace(); 4. array_replace(); 5. array_replace_recursive(); 6. array_splice().

What are the find and replace functions in php

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 ] )
Copy after login

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 = &#39;hello,world,hello,world&#39;;
$replace = &#39;hi&#39;;
$search = &#39;hello&#39;;
echo str_ireplace($search, $replace, $str);
?>
Copy after login

The output result of executing the above code is:

hi,world,hi,world
Copy after login

substr_replace() function

substr_replace( ) The syntax of the function is as follows:

mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )
Copy after login

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 = &#39;hello,world,hello,world&#39;;
$replace = &#39;hi&#39;;
echo substr_replace($str, $replace, 0,5);
?>
Copy after login

The execution result of the above code is:

hi,world,hello,world
Copy after login

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 ]] )
Copy after login

Parameter description:

  • arr represents an array.
  • start indicates the position (subscript) where deletion begins:
    • If start is a positive number, delete from front to back.
    • If start is a negative number, start from the position -start from the end of arr and delete from back to front. For example -2 means start from the second to last element of the array.
  • length is an optional parameter, indicating the number of elements to be deleted:
    • If length is a positive number, it means length elements are deleted;
    • If length is a negative number, all elements starting from start and counting down to length from the end of the array will be deleted;
    • If it is omitted, all elements starting from start and ending at the end of the array will be deleted.
  • replacement is an optional parameter indicating the value to be replaced. If replacement has multiple values, it needs to be set to an array. If there is only one value, it does not need to be set to an array.

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");
?>
Copy after login

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
)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template