Home >Backend Development >PHP Problem >How to delete an element from a string in php
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)".
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 = 'hello,world,Hello,world'; echo "原字符串:".$str."<br>"; $search = 'hello'; $replace = ''; echo "需要删除的元素:".$search."<br>"; echo "删除指定值的字符串:".str_replace($search, $replace, $str); ?>
##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.
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello,world,Hello,world'; echo "原字符串:".$str."<br>"; $search = 'hello'; $replace = ''; echo "需要删除的元素:".$search."<br>"; echo "删除指定值的字符串:".str_ireplace($search, $replace, $str); ?>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!