PHP-删除重复的标点符号
DEMO:
<?php
$str = "Hello... how are you!!?? I''m bored!!----!!!&&&&&^^^^%%%(()))((<<<<<";
$str = preg_replace('~((?<!:)[^\p{L}\p{N}])\1+~u', '$1', $str);
// 输出:Hello. how are you!? I'm bored!-!&^%()(<
echo $str.PHP_EOL;
?>正则解释:
[^\p{L}\p{N}] - 匹配除Unicode字母数字字符外的所有字符
(?<!:) - 仅在不以":"开头的情况下进行匹配,以保全 http://...
([^\p{L}\p{N}]) - 在第1组中捕获以上内容以供反向参考
\1+ - 匹配捕获到的#1组中的一个或多个,从而确保匹配两个或多个相同的非字母数字将其替换为 $1 ,即捕获的非字母数字字符
[^\p{L}\p{N}] - Match anything but unicode alphanumeric character
(?<!:) - Match only if not precede by : to take care of http://...
([^\p{L}\p{N}]) - Capture above in a group #1 for back-reference
\1+ - Match one or more of captured group #1, thus making sure 2 or more of same non-alphanumeric is matched Replace it by $1 i.e. the captured non-alphanumeric character
原文章:https://ask.csdn.net/questions/775111