php将字符串中连续的某个字符替换为一个

 
更多

php将字符串中连续的某个字符替换为一个。

/**
 * php将字符串中连续的某个字符替换为一个
 * @param string $search
 * @param string $replace
 * @param string $subject
 * @return string
 */
function str_replace_multiple_consecutive($search, $replace, $subject) {
    return (string)preg_replace("/[" . $search . "]+/i", $replace, $subject);
}

示例:

$str = ",,,,www.####phpernote##.com,,,,我喜欢,,,,我很###喜欢#!,,,";

$str1 = str_replace_multiple_consecutive(',', ',', $str);
$str2 = str_replace_multiple_consecutive(',', ',', $str1);
$str3 = str_replace_multiple_consecutive('#', '', $str2);

echo "处理前:" . $str, '<br />';
//处理前:,,,,www.####phpernote##.com,,,,我喜欢,,,,我很###喜欢#!,,,
echo "处理后:" . $str1, '<br />';
//处理后:,,,www.####phpernote##.com,,,,我喜欢,,,我很###喜欢#!,,,
echo "处理后:" . $str2, '<br />';
//处理后:,www.####phpernote##.com,我喜欢,我很###喜欢#!,
echo "处理后:" . $str3, '<br />';
//处理后:,www.phpernote.com,我喜欢,我很喜欢!,
打赏

本文固定链接: https://www.cxy163.net/archives/3262 | 绝缘体

该日志由 绝缘体.. 于 2020年05月18日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: php将字符串中连续的某个字符替换为一个 | 绝缘体
关键字: , , , ,

php将字符串中连续的某个字符替换为一个:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter