Home  >  Article  >  Backend Development  >  How to delete an element from a string in php

How to delete an element from a string in php

青灯夜游
青灯夜游Original
2022-03-07 11:46:453728browse

php method to delete an element in a string: 1. Use str_replace(), syntax "str_replace("value to be deleted","",$str)"; 2. Use str_ireplace(), Syntax "str_ireplace("value to be deleted","",$str)".

How to delete an element from a string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Delete characters in php An element in the string

Method 1: Use the str_replace() function

The str_replace() function replaces elements in the string with other characters Some characters (case-sensitive); when the replacement value is the empty string "", the specified value can be deleted.

Syntax:

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. Variable counting the number of substitutions.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;hello,world,Hello,world&#39;;
echo "原字符串:".$str."<br>";

$search = &#39;hello&#39;;
$replace = &#39;&#39;;

echo "需要删除的元素:".$search."<br>";
echo "删除指定值的字符串:".str_replace($search, $replace, $str);
?>

How to delete an element from a string in php

##Method 2: Use str_ireplace() function

str_ireplace() function replaces some characters in the string (not case-sensitive); similarly, when the replacement value is the empty string

"", the specified value can be deleted.

The str_ireplace() function has similar syntax to the str_replace() function, please refer to the above.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;hello,world,Hello,world&#39;;
echo "原字符串:".$str."<br>";

$search = &#39;hello&#39;;
$replace = &#39;&#39;;

echo "需要删除的元素:".$search."<br>";
echo "删除指定值的字符串:".str_ireplace($search, $replace, $str);
?>

How to delete an element from a string in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to delete an element from a string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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