Forum Moderators: coopster

Message Too Old, No Replies

Remove double letters in a text string

Regular Expression Problem

         

rfontaine

7:10 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



Hi,

I have alot of text with double letters. Something like:

"add" should be "ad"

"allot" should be "alot"

ETC.

Being terrible at regular expressions I am looking for a preg_replace that replaces these double letters with just a single letter.

Any ideas on how this can be accomplished? It seems like a toughie -

Thank you in advance,
Ron

rfontaine

8:10 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



anyone?

coopster

8:50 pm on Sep 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Using a back reference [php.net] would work.

$string = "Hello there dubble letters!"; 
$pattern = "/([a-z])\\1/Uis";
$replacement = "$1";
print "$string<br />";
print preg_replace($pattern, $replacement, $string);

rfontaine

8:53 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



Well THAT is cool Coopster...didn't know about that, thank you Very much!

ergophobe

6:26 pm on Sep 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, but this is probably just not a job for regex unless you have a very particular situation (also note that "allot" is a valid English word, but "alot" is not). In the following versions, errors are bolded.

"I want to allot some serious money to adding a star-studded add campaign to our program that will help allot".

becomes

"I want to alot some serious money to ading a star-studed ad campaign to our program that will help alot".

Your regex takes a sentence with two errors and creates a sentence with four errors.

The correct version (uhh, in terms of spelling - it's still a terrible sentence of course) would be

"I want to allot some serious money to adding a star-studded ad campaign to our program that will help a lot".

rfontaine

5:45 pm on Sep 30, 2004 (gmt 0)

10+ Year Member



Hi ergophobe,

It was just an example used to explain whatI needed - there is actually a larger problem that needed solving and figuring out how to do this particular thing is one step.

ergophobe

7:05 pm on Sep 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I figured that was just an example, but the same caveat applies. You have to make sure that you're not going to be grabbing all sorts of stuff you don't expect.

have fun!