2013-02-11 76 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