How to delete a specified substring in a string in php

青灯夜游
Release: 2023-03-13 09:56:01
Original
2406 people have browsed it

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)".

How to delete a specified substring in a string in php

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)
Copy after login
ParameterDescription
find Required. 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.

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 = &#39;hello,world,Hello,World&#39;;
$replace = &#39;&#39;;
$search = &#39;hello&#39;;
echo str_replace($search, $replace, $str);
?>
Copy after login

Output result:

How to delete a specified substring in a string in php

Example 2: Use the str_ireplace() function to remove the specified substring "hello"

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;hello,world,Hello,World&#39;;
$replace = &#39;&#39;;
$search = &#39;hello&#39;;
echo str_ireplace($search, $replace, $str);
?>
Copy after login

Output result:

How to delete a specified substring in a string in php

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!

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