I see a lot of questions posted about how to handle textarea input -- often from people who forget the rules of HTML and don't understand why line enders disappear.
The following code takes input, converts line enders to paragraph tags, and runs the result through tidy to clean up any bad HTML. The result is a properly formed HTML fragment with paragraphs handled properly.
Since PHP5 includes tidy functions, I was able to cut a fairly long piece of code that called an external tidy binary down to just a few lines.
function validate($input)
{
if (get_magic_quotes_gpc()) $input = stripslashes($input);
$input = strip_tags($input,'<blockquote><ul><li><a><b><i><img><br><u><p><em>');
$input = str_replace("\n", '<p>', $input);
$tidycfg = array('show-body-only' => TRUE, 'enclose-text' => TRUE);
$input = tidy_repair_string($input, $tidycfg);
return addslashes($input);
}
The following code takes input, converts line enders to paragraph tags, and runs the result through tidy to clean up any bad HTML. The result is a properly formed HTML fragment with paragraphs handled properly.
Since PHP5 includes tidy functions, I was able to cut a fairly long piece of code that called an external tidy binary down to just a few lines.
function validate($input)
{
if (get_magic_quotes_gpc()) $input = stripslashes($input);
$input = strip_tags($input,'<blockquote><ul><li><a><b><i><img><br><u><p><em>');
$input = str_replace("\n", '<p>', $input);
$tidycfg = array('show-body-only' => TRUE, 'enclose-text' => TRUE);
$input = tidy_repair_string($input, $tidycfg);
return addslashes($input);
}