Home > Article > Backend Development > How to replace a string with a given substring in php
Replacement method: 1. Use the "substr_replace()" method to replace part of the string with another string; 2. Use the "str_replace()" method to achieve replacement; 3. Use "str_ireplace()" The method can be replaced.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Method 1: Use substr_replace() function
substr_replace() function replaces part of a string with another string.
The syntax of the substr_replace() function is as follows:
mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )
This function accepts three required parameters $string
, $replacement
(replacement value) and $start
(the position where the replacement starts), and an omissible parameter $length
(the number of characters to be replaced). The
substr_replace() function makes a copy of the string $string
a substring qualified by the $start
and $length
parameters. Strings are replaced using $replacement
.
To put it simply, use the $replacement
value to start from the $start
position (note, the string position starts from 0), Replace $length
characters.
Example:
<?php $str = 'hello,world,hello,world'; $replace = '***'; echo substr_replace($str, $replace, 0,5)."<br>"; echo substr_replace($str, $replace, 6,5)."<br>"; ?>
##Method 2: Use str_replace() function
str_replace() function replaces some characters in a string with other characters (case sensitive). Syntax:str_replace($search,$replace,$string,$count)This function can replace some characters in the string in a case-sensitive manner; the function accepts three required parameters $search (substring to be searched), $replace (the value to be replaced), $string (a string), and an optional parameter $count (a variable).
Example:
<?php $str = 'hello,world,Hello,World'; $replace = '***'; $search1 = 'hello'; $search2 = 'world'; echo str_replace($search1, $replace, $str)."<br>"; echo str_replace($search2, $replace, $str)."<br>"; ?>
Method 3: Use str_ireplace() function
The str_replace() function replaces some characters in a string (case sensitive). Syntax:str_ireplace($search,$replace,$string,$count)This function can ignore some characters in the replacement string, ignoring case. The parameters accepted by this function are the same as the str_replace() function. The first three parameters are Required (cannot be omitted), the latter parameter $count is omissible (but if set, the number of substitutions can be obtained). The parameter $count can accept a variable to count the number of substitutions; if this parameter is set, you can know how many substitutions have been performed in total. Example:
<?php header("Content-Type: text/html;charset=utf-8"); //设置字符编码 $str = 'hello,world,Hello,World'; $replace = '***'; $search1 = 'hello'; $search2 = 'world'; echo str_ireplace($search1, $replace, $str, $i)."<br>"; echo "一共执行了 $i"." 次替换<br><br>"; echo str_ireplace($search2, $replace, $str, $i)."<br>"; echo "一共执行了 $i"." 次替换<br><br>"; ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to replace a string with a given substring in php. For more information, please follow other related articles on the PHP Chinese website!