php将重复的字符替换成一个字符,php将字符串中连续的某个字符替换为一个

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, '
';

//处理前:,,,,www.####phpernote##.com,,,,我喜欢,,,,我很###喜欢#!,,,

echo "处理后:" . $str1, '
';

//处理后:,,,www.####phpernote##.com,,,,我喜欢,,,我很###喜欢#!,,,

echo "处理后:" . $str2, '
';

//处理后:,www.####phpernote##.com,我喜欢,我很###喜欢#!,

echo "处理后:" . $str3, '
';

//处理后:,www.phpernote.com,我喜欢,我很喜欢!,

http://www.dengb.com/PHPjc/1418406.htmlwww.dengb.comtruehttp://www.dengb.com/PHPjc/1418406.htmlTechArticlephp将字符串中连续的某个字符替换为一个 php将字符串中连续的某个字符替换为一个。 /** * php将字符串中连续的某个字符替换为一个 * @par...

你可能感兴趣的:(php将重复的字符替换成一个字符,php将字符串中连续的某个字符替换为一个)