2017-04-13 128 views
-5

在PHP中,如何去掉以@(at)符号开头的单词后面的所有双空格?用PHP正则表达式来替换以@结尾以2个空格结尾的单词

示例输入:

This is an @example input. 

输出示例:

This is an @example output. 
+1

你尝试过这么远吗? –

+1

请阅读[我可以问哪些主题](http://stackoverflow.com/help/on-topic) 和[如何提出一个好问题](http://stackoverflow.com/help/how-to -ask) 和[完美问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) 以及如何创建[最小,完整和可验证示例] (http://stackoverflow.com/help/mcve) ** SO不是**免费的编码或代码转换或调试或教程或库查找服务 ___我们尝试修复您的代码,我们不写您的代码____ – RiggsFolly

+0

通过用一个空格替换多个内联空间,你会失去什么? https://eval.in/775818 – chris85

回答

0

如同一个@开头的单词,复位匹配输出,匹配多于一个空格之后接着做一个替换:

preg_replace('[email protected]\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

相关问题