开发者

How do you replace repetitive text using preg_replace?

开发者 https://www.devze.com 2023-01-11 09:39 出处:网络
I often have comment forms and lots of users post things such as开发者_开发知识库 !!!!!!!!!!! and??????????, and I want to use preg_replace to change them to !! and ??, with two maximum.

I often have comment forms and lots of users post things such as开发者_开发知识库 !!!!!!!!!!! and ??????????, and I want to use preg_replace to change them to !! and ??, with two maximum.

Any idea how to do this?


$t = "aaaaaabbbbbbbbccccccccccc";
$t = preg_replace('~(.)\1{2,}~', '$1$1', $t);
echo $t; // aabbcc
0

精彩评论

暂无评论...
验证码 换一张
取 消