我想用一个Newline字符替换多个Newline字符,用一个空格替换多个空格.
我尝试过但preg_replace("/\n\n+/", "\n", $text);失败了!
我也在$ text上进行格式化.
$text = wordwrap($text, 120, '<br/>', true);
$text = nl2br($text);
Run Code Online (Sandbox Code Playgroud)
$ text是用户为BLOG拍摄的大文本,为了更好的格式化,我使用wordwrap.
我有这样的文本数据:
Learn More
Hide [x]
Colors
Fetching Colors description...
Show more topics
Art exhibitions
Fetching Art exhibitions description...
Abstract art
Fetching Abstract art description...
Representational art
Fetching Representational art description...
Run Code Online (Sandbox Code Playgroud)
我希望能够删除所有回车符,所以它变成这样:
Learn More Hide [x] Colors Fetching Colors description... Show more topics
Run Code Online (Sandbox Code Playgroud)
我如何在 PHP 中执行此操作?请注意,字符串可能是 UTF-8 字符串,我们需要考虑各种制表符、回车符和空格字符。(我正在思考编译器中使用的标记器算法,其中处理多个回车符和空格字符。)