SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql and new lines

    Suppose, this is my variable

    $var = "
    dsaklfjsa
    sadkfljsl;dfjsa skdf kls n ksdjfn smdf sd
    lfsdhf d ,.scdn lkbn lkdsaf slkjdfhsd fnsildfu dfskdfnsndfjygsdf ";


    when i try to insert $var into the dbase.. it looses the \n's.... any reason why? any way around it?

  2. #2
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take it out of the database and print it onto the page, then view the source code and you'll see there are line breaks

    To turn into real line breaks use:

    $var = str_replace("\n", "<br>", $var);

  3. #3
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    duh!

    naa.. there are no \n or <br> or anything like that in the dbase itself.. its like they dun get stored

  4. #4
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use addslashes() for all your data?

  5. #5
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You shouldn't be able to see the \n in the databases, they do get stored though.

  6. #6
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    addslashes wont work.. coz all it does is, add \ before every " and other special characters... nothing esle...

    as for not being able to the \n's... well.. ok... but then but why arnt the right things showing up? why arnt the lines showing up?

    i tried the code you gave me.. it dint work

  7. #7
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmmmm......ok.. the code worked... but isnt there an easier or faster way? coz, there are waaaaay to many files to edit this way

  8. #8
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $var = nl2br($var);
    is about the only other way.
    Kevin

  9. #9
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The draw back of nl2br is that it produces <br>, which if you are creating XHTML compliant pages are not valid. That is why str_replace is better as you can replace the new lines with the compliant <br />.

    On php.net however, in one place they say it replaces it with <br />, and in other places it says <br>.

  10. #10
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are losing the visible newlines because HTML ignores newlines - in HTML you have to have <br> or <br /> tags instead. The newlines are stored in the browser, but when stuff is outputted to HTML they are no longer visible unless you replace them with <br> tags.

    This is a GOOD thing. There are many applications that do NOT require <br> tags to generate new lines - such as e-mail sending. If the data was stored in the database with <br> tags instead of newlines it would make it much harder to use the data in situations other then HTML, such as formatting data for hand held PCs or e-mailing articles to people.


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
  •