2013-02-11 43 views
0

我有一個文本文件,它看起來是這樣的:PHP刪除所有空格後的特定字符

What word is on page 9, 4 words from the left on line 15? account 
What word is on page 11, 2 words from the left on line 5? always 

有沒有一種方法,我可以後刪除所有空間的「?」所以它看起來是這樣的(空間量在整個實際的文本文件中的所有不同):

What word is on page 9, 4 words from the left on line 15?account 
What word is on page 11, 2 words from the left on line 5?always 
+0

瞭解裝飾函數 – samayo 2013-02-11 17:49:09

+0

@PHPNooB修剪函數從字符串的開頭或結尾刪除字符,而不是中間字符。 – 2013-02-11 17:50:34

+0

你需要一個正則表達式來匹配'?'+空格並用'?'替換 - ''\?\ s +''是那個表達。在'preg_replace'中使用它... – Floris 2013-02-11 17:51:17

回答

2

使用正則表達式:

$str = 'What word is on page 9, 4 words from the left on line 15? account'; 
echo preg_replace('/\?\s+/', '?', $str); 
+0

+1首先到達 – 2013-02-11 17:53:17

+0

哇!謝謝,它工作完美。你和@JohnConde只相隔了幾秒鐘。 – user1981730 2013-02-11 17:56:41

1
$input = 'What word is on page 9, 4 words from the left on line 15? account'; 
echo preg_replace('/\?\s+/', '?', $input); 

See it in action

相關問題