I am building a function that wraps <p></p> around each paragraph entered into a TextArea.
It is pretty close to working, except that when I do View—> Source, everything is on one super long line?!
How do I put carriage returns or whatever they are called in the Source?
Here is my code…
function nl2p($string, $line_breaks = true) {
// Remove existing HTML formatting to avoid double tags.
$string = str_replace(array('<p>', '</p>', '<br>', '<br/>'), '', $string);
// Replace Carriage Return with Empty String.
// Replace multiple Newlines with closing & opening paragraph tags.
// Replace single Newline with break tag.
if ($line_breaks == true) {
return '<p>'.preg_replace(array("#\\r#","#\
{2,}#", "#\
#"), array("","</p><p>", "<br />"), $string).'</p>';
}else{
return '<p>'.preg_replace("/\
/", "</p>\
<p>", trim($string)).'</p>';
}
}
echo nl2p($text, TRUE);
If I add
into the line below with single quotes, then it just adds a visible
to the source…
return '<p>'.preg_replace(array("#\\r#","#\
{2,}#", “#
#”), array(‘’,'</p><p>
', ‘<br />
‘), $string).’</p>’;
And if I add
into the line with double quotes, then it ends up adding extra carriage returns in my output…
return '<p>'.preg_replace(array("#\\r#","#\
{2,}#“, “#
#”), array(”“,”</p><p>
", "<br />
"), $string).‘</p>’;
I think my preg_replace is the culprit…
Debbie