删除PHP中的各种空白


Removing various sorts of whitespace in PHP

我希望使用正则表达式删除多个换行符。假设我有这个文本:

"On the Insert tab'n 'n'nthe galleries include 'n'n items that are designed"

那么我想把它换成

"On the Insert tab'nthe galleries include'nitems that are designed"

所以我的要求是:

  1. 它将删除所有多个换行符,并替换为一个换行符
  2. 它将删除所有多个空格,并替换为一个空格
  3. 空间也将被修剪

我确实搜索了很多,但找不到解决方案——我得到的最接近的一个是用正则表达式删除多余的换行符。

使用此:

echo trim(preg_replace('#('s)+#',"$1",$string));
$text = str_replace("'r'n", "'n", $text); // converts Windows new lines to Linux ones
while (strpos($text, "'n'n") != false)
    {
        $text = str_replace("'n'n", "'n", $text);
    }

这将对换行符进行排序。

$text = trim($text);
preg_replace('/'s+/', ' ', $text);
preg_replace('/(?:'s*(?:'r'n|'r|'n)'s*){2}/s', "'n", $text);

感谢使用正则表达式删除多余的换行符