newlines and nl





10
Date Submitted Mon. Sep. 18th, 2006 7:26 PM
Revision 1 of 1
Helper ffxfiend
Tags mysql | PHP
Comments 0 comments
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

 

Jeremiah Poisson

rspdesign.net
*** Stay sane inside insanity ***

Comments

There are currently no comments for this snippet.

Voting