我希望为我的网站做同样的事情,就像我在 reddit 上看到的那样。当你\n一次时,它不起作用,你必须做两次\n\n才能得到一个
我试过这个:
$texte = preg_replace('#{\n}#', '', $texte);
$texte = preg_replace('#{\n}{\n}#', '\n', $texte);
$texte = nl2br($texte);
它不起作用......有人可以帮忙吗?
$str = preg_replace('~(*BSR_ANYCRLF)\R(\R?)~', '$1', $str);
\R
with 该(*BSR_ANYCRLF)
选项匹配任何 CRLF 类型的换行符序列(删除该选项以匹配所有 Unicode 换行符)。
\R(\R)?
将导致$1 = '\R'
两个换行符和$1 = ''
一个换行符。
如果您只有一个,上述内容将完全删除换行符。这可能不是你想要的。相反,我建议单独保留单个换行符并将双换行符转换为 HTML<br />
标记:
$str = preg_replace('~(*BSR_ANYCRLF)\R{2}~', '<br />', $str);
尝试这个:
$texte = preg_replace('#(\r\n|\r)#', "\n", $texte);
$texte = preg_replace('#\n(?!\n)#', ' ', $texte);
$texte = preg_replace('#\n\n#', "\n", $texte);
$texte = nl2br($texte);
第一行规范了行尾。第二个用\n
空格替换 single s。第三行将 double 替换\n
为一个。