[php]代码库/**
* 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([^\n
", '
'), trim($string)).'
else
return '
'.preg_replace("/([\n]{1,})/i", "
\n", trim($string)).'
';}