Home > Backend Development > PHP Problem > How to replace a string with a given substring in php

How to replace a string with a given substring in php

青灯夜游
Release: 2023-03-12 17:44:01
Original
8029 people have browsed it

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.

How to replace a string with a given substring in php

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

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

How to replace a string with a given substring in php

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

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 = &#39;hello,world,Hello,World&#39;;
$replace = &#39;***&#39;;
$search1 = &#39;hello&#39;;
$search2 = &#39;world&#39;;
echo str_replace($search1, $replace, $str)."<br>";
echo str_replace($search2, $replace, $str)."<br>";
?>
Copy after login

How to replace a string with a given substring in php

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

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 = &#39;hello,world,Hello,World&#39;;
$replace = &#39;***&#39;;
$search1 = &#39;hello&#39;;
$search2 = &#39;world&#39;;
echo str_ireplace($search1, $replace, $str, $i)."<br>";
echo  "一共执行了  $i"." 次替换<br><br>";

echo str_ireplace($search2, $replace, $str, $i)."<br>";
echo  "一共执行了  $i"." 次替换<br><br>";
?>
Copy after login

How to replace a string with a given substring in php

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!

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