tags:

views:

198

answers:

2

Hi

I need to convert any letter that occur twice or more within a word with a single letter of itself.

For example:

School -> Schol
Google -> Gogle
Gooooogle -> Gogle
VooDoo -> Vodo

I tried the following, but stuck at the second parameter in eregi_replace.

$word = 'Goooogle';
$word2 = eregi_replace("([a-z]{2,})", "?", $word);

If I use \\1 to replace ?, it would display the exact match. How do I make it single letter?

Can anyone help? Thanks

+2  A: 

Not only are you capturing the entire thing (instead of just the first character), but {2,} rematching [a-z] (not the original match). It should work if you use:

$word2 = eregi_replace("(\w)\1+", "\\1", $word);

Which backreferences the original match. You can replace \w with [a-z] if you wish.

The + is required for your Goooogle example (for the JS regex engine, anyway), but I'm not sure why.

Remember that you will need to use the "global" flag ("g").

Richard Szalay
+5  A: 

See http://stackoverflow.com/questions/106067/regular-expression-to-replace-two-or-more-consecutive-characters-by-only-one

By the way: you should use the preg_* (PCRE) functions instead of the deprecated ereg_* functions (POSIX).

Richard Szalay's answer leads the right way:

$word = 'Goooogle';
$word2 = preg_replace('/(\w)\1+/', '$1', $word);
Stefan Gehrig
Dude, that is awesome. I guess I have to switch over to PCRE! Thanks!
uuɐɯǝʃǝs