时间:2021-07-01 10:21:17 帮助过:48人阅读
先来看看nl2br() 函数吧。
定义和用法
nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (
)。
一个简单的例子:
运行结果的HTML代码:
Welcome to
www.scutephp.com
nl2p
nl2br 有个缺点,比如要用CSS做到段落缩进就比较麻烦,这个时候就需要 nl2p 了。将br换行换成段落p换行,比较简单是直接替换:
" . str_replace("\n", "", $text) . "
"; } ?>
比较详细的函数,可以试下:
/** * Returns string with newline formatting converted into HTML paragraphs. * * @param string $string String to be formatted. * @param boolean $line_breaks When true, single-line line-breaks will be converted to HTML break tags. * @param boolean $xml When true, an XML self-closing tag will be applied to break tags (
). * @return string */ function nl2p($string, $line_breaks = true, $xml = true) { // Remove existing HTML formatting to avoid double-wrapping things $string = str_replace(array('', '
', '
', '
'), '', $string); // It is conceivable that people might still want single line-breaks // without breaking into a new paragraph. if ($line_breaks == true) return ''.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("
\n", '
'; else return '
'), trim($string)).''.preg_replace("/([\n]{1,})/i", "
\n", trim($string)).'
'; }