How can you translate this into Regex: Replace any char (aka '.') that is repeating more than once and replace it with one of that char with the exception of "ii" and "iii".
$reg = preg_replace('/(.)/1{1,}/','', $string);
Now this must replace VVVVV
with V
or ....
with .
and must not replace Criiid
(or Criid
) but Criiiiiiid
with Crid
.
Feel free to comment if you don't understand the question.
preg_replace('/([^i])\1+|(i){4,}/', '\1\2', $string)
Note that this will squeeze everything -- spaces, newlines, etc.
The first alternative replaces anything but i
, the second alternative replaces any i
s which occur four times or more:
$reg = preg_replace('/([^i])\1{1,}|(i){4,}/','\1\2', $string);
Or (thanks to @Enissay):
$reg = preg_replace('/([^i])\1+|(i){4,}/','\1\2', $string);