Here are a few features to help you do what you want to do:
function nl2p($str) {
$arr=explode("\n",$str);
$out='';
for($i=0;$i<count($arr);$i++) {
if(strlen(trim($arr[$i]))>0)
$out.='<p>'.trim($arr[$i]).'</p>';
}
return $out;
}
function nl2p_html($str) {
if(strpos($str,'</head>')!==false) {
$out=substr($str,0,strpos($str,'</head>')+7);
$str=substr($str,strpos($str,'</head>')+7);
}
$arr=explode('<',$str);
for($i=0;$i<count($arr);$i++) {
if(strlen(trim($arr[$i]))>0) {
$html='<'.substr($arr[$i],0,strpos($arr[$i],'>')+1);
$sub_arr=explode("\n",substr($arr[$i],strpos($arr[$i],'>')+1));
$paragraph_text='';
for($j=0;$j<count($sub_arr);$j++) {
if(strlen(trim($sub_arr[$j]))>0)
$paragraph_text.='<p>'.trim($sub_arr[$j]).'</p>';
}
$out.=$html.$paragraph_text;
}
}
return $out;
}
The first one, nl2p (), takes a string as input and converts it to an array wherever there is a newline character ( "\n"). Then it goes through each element, and if it finds one that is not empty, it will wrap tags <p></p>around it and add it to the line that returns at the end of the function.
, nl2p_html(), . HTML , <p> </p> , HTML. , , <, HTML-. , , HTML , , .
, ("\n"). , , . , . , HTML-, , .
tl; dr: nl2p() HTML, , nl2p_html() HTML.
HTML , , . , nl2p_html(), W3C, , .
, .