/** * 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,我喜欢,我很喜欢!,