SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Luton, UK
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting text in PHP and MySQL

    Hi All
    I'm just learning PHP and MySQL and I've come across a little problem I'm hoping one of you guys can help with.

    Basically I am building a database that I can put my fictional stories in but what I want to do is keep them in the format that I write them in. i.e. I want to keep all the paragraphs, indents etc.

    I have set up a page in php that submits the story to the database but when I retrieve the story it's just one long text file, no paragraphs....

    I would really appreciate some insight into this.

    Thanks

    Drew
    'As a computer, I find your faith in technology amusing'

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this before you enter it into your db.

    $text = ereg_replace(10, "<br>", $text);

    Also you should read the tutorial by Kevin Yank on webmasterbase.com he covers this stuff
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, when you fill in a form, when you press ENTER, it puts in a line break that is invisable, and represented by this: \n

    When you pull your information out of the database, try using nl2br, and this will convert all the linebreaks (\n which are invisible) to <br> or <br /> depending on your version PHP. Thus then the browser will be able to put them in.

    echo (nl2br($string));

  4. #4
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I say just put a wrap = 'physical' in your text input box then put <pre>$story</pre> tags when you echo the info from the DB.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Luton, UK
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks guys - I'll give them all a go and see what happens!

    Cheers
    Drew
    'As a computer, I find your faith in technology amusing'

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Similar to manipura, I include wrap="hard" in my text area tag to capture the line breaks. But I am pretty ignorant about correct HTML so don't know if one is more correct that the other, or if there is a difference.

    Also, you can use the function nl2br to convert line breaks to <br> tags withing a string:

    string nl2br (string string)

    http://php.net/manual/en/function.nl2br.php


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
  •