【发布时间】:2021-11-29 22:17:32
【问题描述】:
我正在尝试编写一个删除所有空格但保留段落的代码。
我有这个代码:
$replaced = preg_replace('/\s\s+/', ' ', "1st. line
2nd. line goes here
3rd. line goes there");
echo $replaced;
作为我得到的输出:
1st. line 2nd. line goes here 3rd. line goes there
但应该是这样的:
1st. line
2nd. line goes here
3rd. line goes there
nl2br() 函数对我来说不是一个好的解决方案,因为我需要将此输出放在一个文本区域中。
【问题讨论】:
-
您是在浏览器中还是在 CLI 中获得该输出?如果在浏览器中查看源代码或根据需要将输出放在
<pre></pre>或文本区域中。浏览器不会将\n显示为页面上的换行符 -
我在浏览器中获得了该输出,我想在带有换行符的浏览器中获得相同的输出。
标签: php regex preg-replace