我想用一个换行符替换多个换行符,用一个空格替换多个空格。
I tried preg_replace("/\n\n+/", "\n", $text);
并失败了!
我还在 $text 上完成这项工作以进行格式化。
$text = wordwrap($text, 120, '<br/>', true);
$text = nl2br($text);
$text 是从 BLOG 用户那里获取的大文本,为了更好的格式,我使用了 wordwrap。
理论上,正则表达式确实有效,但问题是并非所有操作系统和浏览器仅在字符串末尾发送 \n 。许多人还会发送\r。
Try:
我已经简化了这一点:
preg_replace("/(\r?\n){2,}/", "\n\n", $text);
并解决某些仅发送 \r 的问题:
preg_replace("/[\r\n]{2,}/", "\n\n", $text);
根据您的更新:
// Replace multiple (one ore more) line breaks with a single one.
$text = preg_replace("/[\r\n]+/", "\n", $text);
$text = wordwrap($text,120, '<br/>', true);
$text = nl2br($text);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)