【问题标题】:PHP regex to replace words starting with @ end ending with 2 spaces [closed]PHP正则表达式替换以@结尾以2个空格结尾的单词[关闭]
【发布时间】:2017-09-09 14:49:25
【问题描述】:

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

示例输入:

This is  an @example  input.

示例输出:

This is  an @example output.

【问题讨论】:

标签: php regex blade laravel-blade


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    相关资源
    最近更新 更多