2017-04-13 56 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

相關問題