Home  >  Article  >  Backend Development  >  How to remove certain substrings from a string in php

How to remove certain substrings from a string in php

青灯夜游
青灯夜游Original
2022-04-26 21:03:092011browse

In PHP, you can use the str_replace() function to remove multiple specified substrings in a string. You only need to set the first parameter of the function to an array containing multiple values. The second Just set each parameter to a null character; the syntax "str_replace(array,'',$str)" will replace multiple substrings with null characters.

How to remove certain substrings from a string in php

The operating environment of this tutorial: Windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use the str_replace() function to remove multiple specified substrings from a string.

str_replace(find,replace,string,count)
Parameters Description
find Required . Specifies the value to look for.
replace Required. Specifies a value that replaces the value in find.
string Required. Specifies the string to be searched for.
count Optional. A variable counting the number of substitutions.

Generally, the str_replace() function only searches for one value and replaces it with the specified value.

But you only need to set the first parameter of the function find to an array containing multiple search values, and set the second parameter to a null character to remove multiple characters in the string. specified substring.

<?php
$str = "a-bc-he-lloa-bc-";
echo $str."<br>";
$arr=[&#39;a-&#39;,&#39;c-&#39;,&#39;e-&#39;];
$newStr=str_replace($arr,&#39;A&#39;,$str);
echo $newStr."<br>";
?>

How to remove certain substrings from a string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove certain substrings from a string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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