Results 1 to 4 of 4

Thread: Formatting blocks of text from MySQL database

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Formatting blocks of text from MySQL database

    I am creating a database with various products in.
    One of the fields will be a desciption field.

    Most of the text needs line breaks in.
    Do I edit the text to include <br />'s before i enter it into the database? or is there some kind of PHP function that will include line breaks after it's called.

    How would this post look in a database? Would you see all the <br />'s?

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You could use nl2br before posting to the db (this would convert any newlines in the textarea into <br /> tags before inserting into the db).

    Example:

    Text like this:
    Code:
    This is
    a
    test
    will be converted to something like this in the db.

    Code:
    This is<br />
    a<br />
    test
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Thanks, i'll give that a go.

  4. #4
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    If you need the text to be formatted properly (i.e. if you have a w3c validated page) a preg_replace would work better. If not, the nl2br is a lot easier to use.
    PHP Code:
    $text preg_replace("/(.*)\n/U","<p>$1</p>",$text."\n");
    #the ."\n" ensures the last line is matched --------^ 
    I think that is the correct regular expression. Anyway, that would put each line in between paragraph tags, making it valid HTML/XHTML.
    Last edited by Jas; 04-09-2008 at 07:46 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •