newlines and nl
10
Here is a pair of function to use in combination. The first one will change newlines into tags to store into a database. The second one does the reverse so you can edit the content of the database without having the tags show when you edit the content. If you can find improvements or have comments please let me know
function nl2p($text){
// Return if there are no line breaks.
if (!strstr($text, "\n")) {
return $text;
}
// put all text into <p> tags
$text = '<p>' . $text . '</p>';
// replace all newline characters with paragraph
// ending and starting tags
$text = str_replace("\n", "</p>\n<p>", $text);
// remove empty paragraph tags & any cariage return characters
$remove = array("\r", "<p></p>");
$text = str_replace($remove,"", $text);
return $text;
} // end nl2p
function p2nl($text){
// replace all <p> and </p> tags with a newline characters
// ending and starting tags
$text = str_replace("</p>\n\n<p>", "\n\r", $text);
// remove <p> tag from begining and </p> tag from end of string
$remove = array("<p>", "</p>");
$text = str_replace($remove, "", $text);
return $text;
} // end p2nl
Comments
Voting
Votes Up
ColdKeyboard
ctiggerf
dannyboy
i_kenneth
Pio
SecondV
Sonsam
sphearion
sundaramkumar
wiz1705






There are currently no comments for this snippet.