Tech163
Tech163

Reputation: 4296

How do you replace repetitive text using preg_replace?

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?

Upvotes: 1

Views: 147

Answers (1)

user187291
user187291

Reputation: 53960

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

Upvotes: 4

Related Questions