PHP正则表达式替换以@结尾以2个空格结尾的单词

时间:2017-04-13 12:57:23

标签: php regex blade laravel-blade

在PHP中,如何在一个以@(at)符号开头的单词后面直接删除所有双空格?

示例输入:

This is  an @example  input.

示例输出:

This is  an @example output.

1 个答案:

答案 0 :(得分:0)

匹配以@开头的单词,重置匹配输出,然后匹配多个空格,然后进行替换:

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

正则表达式解释:

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

PHP live demo