[Solved] PHP regex to replace words starting with @ end ending with 2 spaces [closed]


Match a word beginning with a @, reset matching output, match more than one whitespace afterwards then do a replacement:

preg_replace('~@\w+\K\s{2,}~', ' ', $input_string);

Regex explanation:

@\w+    # Match a `@` fowlling a word
\K      # Reset matching process output
\s{2,}  # Match double or more whitespaces

PHP live demo

solved PHP regex to replace words starting with @ end ending with 2 spaces [closed]