Method: 1. Use str_replace() to delete in a case-sensitive manner, syntax "str_replace(substring,'', string)"; 2. Use str_ireplace() to delete in a case-insensitive manner Delete, syntax "str_ireplace(substring,'',string)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, I want to delete the string The specified substring can be replaced by replacing the specified substring in a string with the empty string ''
.
To replace a specified substring, you can use the str_ireplace() and str_replace functions. They will use a new string to replace the specific string specified in the original string. str_replace is case-sensitive, and str_ireplace() is not size-sensitive. Write, the syntax of the two is similar:
str_replace(find,replace,string,count) str_ireplace(find,replace,string,count)
Parameter | 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. |
Just set the replace parameter value to the empty string ''
.
Example 1: Use the str_replace() function to remove the specified substring "hello"
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello,world,Hello,World'; $replace = ''; $search = 'hello'; echo str_replace($search, $replace, $str); ?>
Output result:
Example 2: Use the str_ireplace() function to remove the specified substring "hello"
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello,world,Hello,World'; $replace = ''; $search = 'hello'; echo str_ireplace($search, $replace, $str); ?>
Output result:
Recommended learning : "PHP Video Tutorial"
The above is the detailed content of How to delete a specified substring in a string in php. For more information, please follow other related articles on the PHP Chinese website!