SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Switzerland
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How can I make paragraphs possible ...?

    I have a form with a text-field. If I type some text (with paragraphs) in it and save it (the text is now going to be stored in a MySQL db) there are no paragraphs left when I display the stored text on a page ... Why???

    Thanks for all helpful advice!

    tg

  2. #2
    SitePoint Zealot hpnadig's Avatar
    Join Date
    Sep 2003
    Location
    Bangalore
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is because your database doesn't take in newline (\n) and converts it to space instead...
    Home Page| Blog | Gallery
    sys op & contributor @ kn wikipedia

    Still stuck with a stinky browser? Go get FireFox

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Switzerland
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and how can I change this???

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Either the database or the browser is converting \n characters. Try this (replace $text with your variable) and see if it changes anything.
    PHP Code:
    <p><?php echo str_replace("\n\n""</p><p>"$text);?></p>

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Switzerland
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that works :-) THANKs!

  6. #6
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    Either the database or the browser is converting \n characters. Try this (replace $text with your variable) and see if it changes anything.
    PHP Code:
    <p><?php echo str_replace("\n\n""</p><p>"$text);?></p>
    I think it's much easier (and faster) to accomplish the same thing with nl2br() ...

    stay good
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  7. #7
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nl2br doesn't insert <p>, only <br />...
    - website

  8. #8
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In a scenario where someone types text in a form, and separates his paragraphs by pressing 'enter' key, you can use <p> html tag to present these \n chars, but practically the same thing is accomplished by using <br /> for each '\n' ...

    So, my reccomendation is that for this specific issue nl2br() is the right choice, nothing more...

    p.s. One question for SitePoint admins: I'm curious how does QuickReply form handles '\n' ??
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  9. #9
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by boccio
    In a scenario where someone types text in a form, and separates his paragraphs by pressing 'enter' key, you can use <p> html tag to present these \n chars, but practically the same thing is accomplished by using <br /> for each '\n' ...

    So, my reccomendation is that for this specific issue nl2br() is the right choice, nothing more...
    Which is more proper though, representing paragraphs with paragraph tags, or two hard breaks? I'd go for the paragraphs personally.

  10. #10
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, also adds possibility for style sheets to modify eg the first line/letter in each paragraph, something that would not be possible if you use nl2br
    - website

  11. #11
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    yes, also adds possibility for style sheets to modify eg the first line/letter in each paragraph, something that would not be possible if you use nl2br
    ok, argument accepted. as for myself, so far I handled form inputs (text areas) with nl2br, but...
    it's never too late to admit you're wrong...
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko


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
  •