I am trying to write a function that removes consecutive duplicate words within a string. It's vital that one any matches found by the regular expression remains. In other words...
A very very very dirty dog
should become...
A very dirty dog
I have a regular expression that seems to work well (based on this post)
(\b\S+\b)(($|\s+)\1)+
However I'm not sure how to use preg_replace (or if there's a better function) to implement this. Right now I have it deleting all matching repeated words without leaving one copy of the word intact. Can I parse a variable or special instruction to it to keep a match ?
I have this currently...
$string=preg_replace('/(\b\S+\b)(($|\s+)\1)+/', '', $string);