How to remove certain substrings from a string in php

青灯夜游
Release: 2023-03-15 20:54:02
Original
1973 people have browsed it

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)
Copy after login
ParametersDescription
findRequired . Specifies the value to look for.
replaceRequired. Specifies a value that replaces the value in find.
stringRequired. 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>";
?>
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!