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

青灯夜游
青灯夜游 原创
2023-03-11 11:08:01 1248浏览

在php中,可以利用substr_replace()函数来删除字符串中多个字符,只需要从指定位置开始,将指定数目的字符替换为空字符即可;语法格式“substr_replace(字符串,'',开始位置,替换数目)”。

本教程操作环境: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中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。