输入banner图图片脚本导航/分类

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怎么将指定字符串替换为空的详细内容,更多请关注gxlcms其它相关文章!