Why the following won't work?
<?php
$text = 'Hell looo w orlldddddd!!!!!!!!!';
$sanitized = preg_replace("/(\w|\s)\1{1,}/mi","$1",$text);
echo $sanitized;
?>
The output expected should be : Hel lo w orld!
Thank you
Try this:
$text = 'Hell looo w orlldddddd!!!!!!!!!';
$sanitized = preg_replace('/(\w|\s|.)\\1+/', '$1',$text);
echo $sanitized;
Outputs:
Hel lo w orld!
This is the best you can do with the regex in this case.
Helo world!Hel and lo and not to remove them bitween lo and wHell lo -> Helo can be explained with l being the same character after whitespace. But yes, in the end, you can't really get Helo world!, but you can do better than Hel lo w orld!You forgot the slash inside the regex:
/(\w|\s)/\1{1,}/mi
/(\w|\s)/\1{1,}/mi - Did you test it? I did. Not working!echo preg_replace('/([:])\1+/', '$1', 'a:b::c'); outputs a:b:c
Helo w orld!. There is ambiguity for the space afterw, since it'sw o, why should the white space be removed altogether in the case ofw o, but not in the case ofo w?