#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0

    Question Preserving new line characters


    I have a form with a text area where users can enter text and submit.

    However, when I display the data that the user submitted, it doesn't preserve the formatting. What I mean is, the new line characters seem to just get ignored even though it's in the DB.

    This post is a good example of what I want to happen. Preserving all 4 paragraphs when displayed to the forum.

    Any ideas? Thanks in advance!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Am I right to presume you are displaying this in an html page? If so then the problem is not that the formatting is being lost, but it is not being displayed.

    HTML does not display whitespace, converting runs of whitespace (including spaces, tabs, linefeeds, carriage returns etc) into a single space. You will need to replace the line endings with <br> tags, and if you want to preserve indentation you will need to replace spaces with &nbsp;.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0
    Originally Posted by DevCoach
    Am I right to presume you are displaying this in an html page? If so then the problem is not that the formatting is being lost, but it is not being displayed.

    HTML does not display whitespace, converting runs of whitespace (including spaces, tabs, linefeeds, carriage returns etc) into a single space. You will need to replace the line endings with <br> tags, and if you want to preserve indentation you will need to replace spaces with &nbsp;.

    Dave - The Developers' Coach

    Thanks Dave,

    Did what you said and it worked perfect. Did something like this:

    var = var.replace("\n","<br>")

    Just curious, is there another way to do this using a library or some other function? Something like an encode or decode sort of thing?

IMN logo majestic logo threadwatch logo seochat tools logo