找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 查看内容

php怎么删除字符串中的指定子串

作者:模板之家 2021-10-14 20:34 108人关注

方法:1、利用str_replace()以区分大小写的方式删除,语法“str_replace(子串,'',字符串)”;2、利用str_ireplace()以不区分大小写的方式删除,语法“str_ireplace(子串,'',字符串)”。

方法:1、利用str_replace()以区分大小写的方式删除,语法“str_replace(子串,'',字符串)”;2、利用str_ireplace()以不区分大小写的方式删除,语法“str_ireplace(子串,'',字符串)”。

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

在php中,想要删除字符串中的指定子串,可以通过对一个字符串中的指定子串进行替换,将其替换成空字符串''即可。

而替换指定子串,可以使用str_ireplace() 和 str_replace 函数,它们都会使用新的字符串替换原来字符串中指定的特定字符串,str_replace 区分大小写,str_ireplace() 不区分大小写,两者语法相似:

str_replace(find,replace,string,count)
str_ireplace(find,replace,string,count)
参数描述
find必需。规定要查找的值。
replace必需。规定替换 find 中的值的值。
string必需。规定被搜索的字符串。
count可选。一个变量,对替换数进行计数。

只需要将replace参数值设置为空字符串''即可。

示例1:利用str_replace()函数去除指定子串“hello”

<?php
header("Content-type:text/html;charset=utf-8");
$str = 'hello,world,Hello,World';
$replace = '';
$search = 'hello';
echo str_replace($search, $replace, $str);
?>

输出结果:

1.png

示例2:利用str_ireplace()函数去除指定子串“hello”

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

输出结果:

2.png

推荐学习:《PHP视频教程》

以上就是php怎么删除字符串中的指定子串的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
来自: 网络收集

全部回复(0)