Home > Article > Backend Development > How to remove specified string in php
php method to remove the specified string: first create a PHP sample file; then define the string; finally pass "str_replace(array("_","="," "),"",$str );" method to remove the specified string.
Recommended: "PHP Video Tutorial"
Using regular expressions can solve the problem, but if you are using it in a project If so, you have to consider the efficiency of the code. Obviously, the efficiency of regular expressions is very low. If you can't use regular expressions, don't use them. Remember!
Just like the current problem, you can do this Write:
<?php $str = "我_们_的_=家+园"; $str = str_replace(array("_","=","+"),"",$str); echo $str; ?>
Isn’t it much simpler to write like this and eliminate the efficiency problem of regular matching?!!
Related introduction:
The str_replace() function replaces some characters in a string (case sensitive).
This function must follow the following rules:
If the searched string is an array, then it will return an array.
If the searched string is an array, then it will find and replace each element in the array.
If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings.
If you search an array and replace only one string, the replacement string will work on all found values.
Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.
Note: This function is binary safe.
Syntax
str_replace(find,replace,string,count)
Parameters
find Required. Specifies the value to look for.
replace Required. Specifies the value to replace the value in find.
string Required. Specifies the string to be searched for.
count Optional. A variable counting the number of substitutions.
The above is the detailed content of How to remove specified string in php. For more information, please follow other related articles on the PHP Chinese website!