PHP删除特定字符后的所有空格

时间:2013-02-11 17:47:11

标签: 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

2 个答案:

答案 0 :(得分:2)

使用正则表达式:

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

答案 1 :(得分: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