因为 nl2br 回车的话是不会在开头空两格的,但是P标签可以CSS样式开头空两格。
添加php函数,因为nl2p在php中不能直接使用。
function nl2p($string, $line_breaks = true, $xml = true){
$string = str_replace(['<p>', '</p>', '<br>', '<br />'], '', trim($string));
if ($line_breaks == true) {
return '<p>' . preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '<br' . ($xml == true ? ' /' : '') . '>'), $string) . '</p>';
} else {
return '<p>' . preg_replace("/([\n|\r\n])/i", "</p>\n<p>", $string) . '</p>';
}}
然后就可以在php中和nl2br()一样使用了,nl2p($text,false);
文章介绍了如何在PHP中创建一个名为nl2p的函数,该函数类似于nl2br,但能处理段首空格。通过使用正则表达式,nl2p函数将多行文本转换为HTML的<p>标签,支持段落和换行,并且可以根据需求调整是否保留换行符。

174

被折叠的 条评论
为什么被折叠?



