php怎么删除字符串中多个字符插图

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

php 删除字符串中多个字符

可以利用substr_replace()函数从指定位置开始,将指定数目的字符替换为空字符即可。

代码示例如下:

<?php
$str = 'hello,world,hello,world';
$replace = '';
echo substr_replace($str, $replace, 0,5);
?>

输出:

,world,hello,world

说明:

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视频教程

以上就是php怎么删除字符串中多个字符的详细内容,更多请关注亿码酷站其它相关文章!



php怎么删除字符串中多个字符
—–文章转载自PHP中文网如有侵权请联系ymkuzhan@126.com删除

云服务器推荐