Home >Backend Development >PHP Problem >How to remove certain substrings from a string in php
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.
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=['a-','c-','e-']; $newStr=str_replace($arr,'A',$str); echo $newStr."<br>"; ?>
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!