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("/([ ]{2,})/i", "/([^>]) ([^<])/i"), array("</p> <p>", '<br'.($xml == true ? ' /' : '').'>'), trim($string)).'</p>'; else return '<p>'.preg_replace("/([ ]{1,})/i", "</p> <p>", trim($string)).'</p>'; }