php怎么将指定字符串替换为空
时间:2021-06-23 来源:互联网
今天PHP爱好者给大家带来php怎么将指定字符串替换为空的方法:1、使用str_ireplace(),语法“str_ireplace(指定子字符串, '', 原字符串)”;2、使用substr_replace(),语法“substr_replace(原字符串,'',开始替换的位置,替换长度)”。希望对大家有所帮助。

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑
在PHP中,可以通过str_ireplace() 和 str_replace()函数 使用新的字符串替换原来字符串中指定的特定字符串,str_replace 区分大小写,str_ireplace() 不区分大小写。
1、使用str_ireplace()函数替换字符串为空
str_ireplace() 的语法如下:
mixed str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 用 replace 替换(忽略大小写)之后的结果。参数 count 表示执行替换的次数。
示例:替换字符串为空
<?php
$str = 'hello,world,hello,world';
$replace = '';
$search = 'hello';
echo str_ireplace($search, $replace, $str);
?>
输出:
,world,,world
2、使用substr_replace() 函数替换字符串为空
substr_replace() 函数的语法如下:
mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )
substr_replace() 在字符串 string 的副本中将由 start 和可选的 length 参数限定的子字符串使用 replacement 进行替换。
如果 start 为正数,替换将从 string 的 start 位置开始。如果 start 为负数,替换将从 string 的倒数第 start 个位置开始。
如果设定了 length 参数并且为正数,就表示 string 中被替换的子字符串的长度。如果设定为负数,就表示待替换的子字符串结尾处距离 string 末端的字符个数。如果没有提供此参数,那么默认为 strlen(string)(字符串的长度)。当然,如果 length 为 0,那么这个函数的功能为将 replacement 插入 string 的 start 位置处。
示例:替换字符串为空
<?php
$str = 'hello world!';
$replace = '';
echo substr_replace($str, $replace, 0,5);
?>
输出:
world!
以上就是php怎么将指定字符串替换为空的详细内容,更多请关注php爱好者其它相关文章!
-
像素火影u鼬神2026最新唤境入口-像素火影网页版秒玩唤境地址 2026-01-17 -
高德扫街榜入口指南-高德扫街榜使用方法与查看位置 2026-01-17 -
小鸡庄园今天答案2026.1.15 2026-01-17 -
梅西是什么梗?揭秘足坛巨星爆火网络的有趣梗源,球迷必看! 2026-01-17 -
永劫无间手游充值入口官网在哪-官网充值入口地址一键获取 2026-01-17 -
"太常寺"是什么机构 蚂蚁新村1月16日答案最新 2026-01-17