Forum Moderators: coopster

Message Too Old, No Replies

Regular Expression for Multiple Same Characters

need some help

         

FourDegreez

7:01 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sometimes users will elongate words with repeating characters, ex. grrrrrr or whoaaaaaaa.

To my knowledge, there are no words in the english language where the same character repeats more than twice. So I'm looking for a regular expression that will basically strip out any repeating character beyond two repeats. So grrrrrr would become grr.

Is this possible? I'm not having any luck.

d40sithui

8:00 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



check this out. this is some pretty badass regex. wish i was the orignal author though lol

<?
$content = "grrrrrrrrrrr arggggg loooool";
$pattern = '{([a-zA-Z])\1+}';
$replacement = '$1$1';
echo "<br>".preg_replace($pattern, $replacement, $content);

PHP_Chimp

8:02 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$subject = 'grrrrrr and sommmmething elllse';
$pattern = '%(.)\\1+%i';
$replace = '$1';
$test1 = preg_replace($pattern, $replace, $subject);
echo $test1;

Same thing as above...just got beaten to it ;)

FourDegreez

8:17 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Awesome, thanks guys!

coopster

2:51 pm on Apr 30, 2008 (gmt 0)