[PHP] PHP函数nl2br()与nl2p()函数 →→→→→进入此内容的聊天室

来自 , 2019-07-11, 写在 PHP, 查看 114 次.
URL http://www.code666.cn/view/2d95666e
  1. /**
  2.  * Returns string with newline formatting converted into HTML paragraphs.
  3.  *
  4.  * @param string $string String to be formatted.
  5.  * @param boolean $line_breaks When true, single-line line-breaks will be converted to HTML break tags.
  6.  * @param boolean $xml When true, an XML self-closing tag will be applied to break tags (<br />).
  7.  * @return string
  8.  */
  9. function nl2p($string, $line_breaks = true, $xml = true)
  10. {
  11.     // Remove existing HTML formatting to avoid double-wrapping things
  12.     $string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);
  13.    
  14.     // It is conceivable that people might still want single line-breaks
  15.     // without breaking into a new paragraph.
  16.     if ($line_breaks == true)
  17.         return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '<br'.($xml == true ? ' /' : '').'>'), trim($string)).'</p>';
  18.     else
  19.         return '<p>'.preg_replace("/([\n]{1,})/i", "</p>\n<p>", trim($string)).'</p>';
  20. }
  21.  

回复 "PHP函数nl2br()与nl2p()函数"

这儿你可以回复上面这条便签

captcha