function nl2p($string, $line_breaks = true, $xml = true) { // Remove existing HTML formatting to avoid double-wrapping things $string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string); // It is conceivable that people might still want single line-breaks // without breaking into a new paragraph. if ($line_breaks == true) return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '<br'.($xml == true ? ' /' : '').'>'), trim($string)).'</p>'; else return '<p>'.preg_replace("/([\n]{1,})/i", "</p>\n<p>", trim($string)).'</p>'; }