
Originally Posted by
felgall
One of the differences between ' ' and " " in PHP is which escape characters they recognise. Between ' ' the only escape character recognised is \'
To have the \n escape recognised you must wrap it in " ".
But when I do that in my code it is also inserting carriage returns in the output?! 
Here is a function I wrote that seems to work pretty well, but View--->Source yields everything on one line which is really ugly and a pain.
(I guess viewing the source in a "DOM Tree" in some editors would fix that, but I am old-school and want it working in the browser's source as well.)
Here is my function...
PHP Code:
$text="I decided to start my own business because I want to be my own boss!
My boss is a jerk and never appreciates anything that I do for him, so why put up with the abuse?! He takes me for granted and doesn't appreciate all of my talents.
Running my own business will give me a chance to do things as I see fit...
Line One
Line Two
Line Three
Line Four
Line Eight
Line Ten";
$text2 = htmlentities($text, ENT_QUOTES);
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#", "#\n{3,}#", "#\n{2}#", "#\n#"), array("", "</p><br /><p>", "</p><p>", "<br />\n"), $string).'</p>';
// return '<p>'.preg_replace(array("#\r#","#\n{2,}#", "#\n#"), array("","</p><p>", "<br />\n"), $string).'</p>';
}else{
return '<p>'.preg_replace("#\n#", "</p><p>", trim($string)).'</p>';
}
}
echo nl2p($text2, TRUE);
If you can help me figure out why I get the View--->Source I do, I'd be grateful!!
Thanks,
Debbie
Bookmarks