Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Banned
    Join Date
    Oct 2010
    Posts
    115
    Thanks
    30
    Thanked 0 Times in 0 Posts

    convert \n to <br>

    See title

    How do i do it?

  • #2
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    Context? Code?
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Where? When?

    General answer:
    Code:
    var textWithBrs = textWithNewlines.replace( /\n/g, "<br/>" );
    EDIT: Okay, but you probably do *NOT* want to do this with JavaScript.

    You probably want to submit the <form> to the server and then let the server-side code do the conversion.

    *USUALLY*, thought, it is even better to *NOT* do this conversion until MUCH LATER. You store the text in the DB with the \n newlines. That way, if you want to present it to the user for editing, you simply plop it back into a <textarea> as is, with the \n's still in place.

    *ONLY* when you finally retrieve it from the DB and want to display it in a <div> or <span> or such, do you *THEN* do the conversion from \n to <br>... and you *STILL* do it in server side coding. You don't send it to the browser with the \n's in place.
    Last edited by Old Pedant; 01-14-2011 at 05:48 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    Thats not javascript then, use some server side language to validate and replace line breaks with a <br /> tag.
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    Probably need to do that with a preg_replace()

    Something like this, but I'm not too good with regex. This is untested.

    Code:
    $clean = preg_replace('/[\n|\r]/m', '<br />', $unclean);
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    When you get the field *OUT* of the database, you just use that replace.

    Example:
    Code:
        echo preg_replace('/[\n\r]/mg', '<br />', $dbrow["someFieldInTable"] );
    I think gnome's answer was not quite correct. You don't use an | or character for choices inside [...]. And he needed a g (global) flag to make sure all occurrences are changed. But I don't use PHP, so I'm not 100% sure on the preg syntax. Just judging by JavaScript and "sed" syntax.
    Last edited by Old Pedant; 01-14-2011 at 06:33 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by maxhudson View Post
    basically, when a form is submitted (textarea) i want it to make <br>s instead of \n for line breaks so it works in html..
    You should use <p> instead of <br />.

    For each newline, replace with a '</p><p>' (that's not a typo).

    Then put a <p> at the very beginning of the string and a </p> at the very end.

    So, where you had this:

    Hi, I'm line one\n
    Yeah and I'm line two\n

    You get this:

    <p>Hi, I'm line one </p><p>Yeah and I'm line two</p>

    This way, you have valid DOM elements, rather than one big string of nothing. With valid elements, you can then select and manipulate them if so desired.

    If the seemingly large space between paragraphs bothers you, you can always get rid of them with CSS:

    {code]
    p {
    margin:0;
    padding:0;
    border:0;
    }
    [/code]

    That will make your paragraphs look like <br /> lines.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by Krupski View Post
    Hi, I'm line one\n
    Yeah and I'm line two\n

    You get this:

    <p>Hi, I'm line one </p><p>Yeah and I'm line two</p>
    The above example is wrong!

    Here is how it would look.

    Hi, I'm line one\n
    Yeah and I'm line two\n

    You get this:

    <p>Hi, I'm line one </p><p>Yeah and I'm line two</p><p></p>

    The reason being the last \n also being changed to </p><p> as well.

    To prevent this use the trim function around the database row.

    like so.
    PHP Code:
    echo preg_replace('/[\n\r]/mg''<br />',trim($dbrow["someFieldInTable"])); 
    Also why use p tags with styling to make it look like a br tag, as this makes the developer make more work for themselves as it does the same job as when you just enter in a <br /> tag. If you need to use each new line in javascript then yes ok but even with the way you say to set it up this still wont work as there is no identifier on the p tags themselves so you would also then need to code more in javascript to be able to get the new line you want.
    Last edited by DJCMBear; 01-14-2011 at 07:41 AM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by DJCMBear View Post
    PHP Code:
    echo preg_replace('/[\n\r]/mg''<br />',trim($dbrow["someFieldInTable"])); 
    Suggest echo preg_replace('/[\n|\r]/mg', '<br />',trim($dbrow["someFieldInTable"]));

  • #11
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    It must be early lol, theres always one mistake in an early code post I got it from post #10 and must have missed that, thanks for pointing it out Philip
    Last edited by DJCMBear; 01-14-2011 at 08:03 AM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    There is no need really to remove the \r\n. They are ignored by HTML.

    Code:
    var val = document.getElementById("textfield").value;
    val = val.replace(/\r|\n/g,"<br/>\r\n");  // NB the Javascript \r\n characters are retained
    val = val.replace(/(<br\/>\.{0,4}<br\/>\r\n)/g,"<br\/>\r\n");  // remove multiple instances

  • #13
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Good suggestion but if your doing it through javascript like your example then it wont matter if they are being removed or not as javascript doesn't change the source codes but only the visuals on the page. On the other hand if done in PHP then yes that is a valid point to make
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Quote Originally Posted by Philip M View Post
    Suggest echo preg_replace('/[\n|\r]/mg', '<br />',trim($dbrow["someFieldInTable"]));

    Unless PHP's preg_replace uses completely different syntax than does JavaScript, sed, bash, etc., the | in there would mean you would replace | characters with <br> as well.

    Ahh...yes, I am correct:
    http://us.php.net/manual/en/regexp.r...er-classes.php

    The | in there is a mistake.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by DJCMBear View Post
    The above example is wrong!

    Here is how it would look.

    Hi, I'm line one\n
    Yeah and I'm line two\n

    You get this:

    <p>Hi, I'm line one </p><p>Yeah and I'm line two</p><p></p>

    The reason being the last \n also being changed to </p><p> as well.
    Actually, you are also wrong. What initially happens is this:

    Hi, I'm line one</p><p>Yeah and I'm line two</p><p>

    What I forgot to mention that I also add the opening <p> and the closing </p> afterwards. My mistake. I END UP with what you posted above... after adding the opening and closing tags.

    Quote Originally Posted by DJCMBear View Post
    Also why use p tags with styling to make it look like a br tag, as this makes the developer make more work for themselves as it does the same job as when you just enter in a <br /> tag. If you need to use each new line in javascript then yes ok but even with the way you say to set it up this still wont work as there is no identifier on the p tags themselves so you would also then need to code more in javascript to be able to get the new line you want.
    Because <p>....text....</p> is a valid DOM entity that can be manipulated whereas ....text<br /> is just a string that has no "identity".

    Not that this is bad...
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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