nl2br function and double spaces?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Sims

    nl2br function and double spaces?

    Hey all,

    So I started using nl2br and love it. But when I convert back from
    <br> to \n, it produces double spaces instead of single space.

    Converting \n to <br>:
    $emailBody=nl2b r($_POST["emailBody"]);
    $emailBody=str_ replace("<br />", "<br>", "$emailBody ");

    Converting <br> to \n:
    $edit["emailBody"]=str_replace("< br>","\n",$edi t["emailBody"]);

    Now what was once single new lines is now doubled.

    Any ideas?

    --Matt
  • Andy Hassall

    #2
    Re: nl2br function and double spaces?

    On 15 Apr 2004 15:31:18 -0700, matt@killermook ie.org (Matthew Sims) wrote:
    [color=blue]
    >Hey all,
    >
    >So I started using nl2br and love it. But when I convert back from
    ><br> to \n, it produces double spaces instead of single space.
    >
    >Converting \n to <br>:
    >$emailBody=nl2 br($_POST["emailBody"]);
    >$emailBody=str _replace("<br />", "<br>", "$emailBody ");
    >
    >Converting <br> to \n:
    >$edit["emailBody"]=str_replace("< br>","\n",$edi t["emailBody"]);
    >
    >Now what was once single new lines is now doubled.
    >
    >Any ideas?[/color]

    nl2br() converts "\n" to "<br />\n".
    You then remove the "<br />", but the "\n" remains, and then you add another.

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

    Comment

    • Matthew Sims

      #3
      Re: nl2br function and double spaces?

      You are correct.

      Shortly after I posted this message, I figured it out.

      $emailBody=str_ replace("\n", "<br>", $_POST["emailBody"]);
      $emailBody=str_ replace("<br>", "\n", $_POST["emailBody"]);

      Don't use nl2br! :)

      Andy Hassall <andy@andyh.co. uk> wrote in message news:<764u70dfd fo979ht96q631o8 m4ao9rr2jh@4ax. com>...[color=blue]
      > On 15 Apr 2004 15:31:18 -0700, matt@killermook ie.org (Matthew Sims) wrote:
      >[color=green]
      > >Hey all,
      > >
      > >So I started using nl2br and love it. But when I convert back from
      > ><br> to \n, it produces double spaces instead of single space.
      > >
      > >Converting \n to <br>:
      > >$emailBody=nl2 br($_POST["emailBody"]);
      > >$emailBody=str _replace("<br />", "<br>", "$emailBody ");
      > >
      > >Converting <br> to \n:
      > >$edit["emailBody"]=str_replace("< br>","\n",$edi t["emailBody"]);
      > >
      > >Now what was once single new lines is now doubled.
      > >
      > >Any ideas?[/color]
      >
      > nl2br() converts "\n" to "<br />\n".
      > You then remove the "<br />", but the "\n" remains, and then you add another.[/color]

      Comment

      • Virgil Green

        #4
        Re: nl2br function and double spaces?

        "Matthew Sims" <matt@killermoo kie.org> wrote in message
        news:1e963607.0 404151829.38b64 657@posting.goo gle.com...[color=blue]
        > You are correct.
        >
        > Shortly after I posted this message, I figured it out.
        >
        > $emailBody=str_ replace("\n", "<br>", $_POST["emailBody"]);
        > $emailBody=str_ replace("<br>", "\n", $_POST["emailBody"]);
        >
        > Don't use nl2br! :)
        >[/color]

        Better still, in my opinion, is to only convert the original when preparing
        for html presentation and don't store it in the original variable. Leave the
        original alone. Why convert back and forth when you don't have to?

        - Virgil


        Comment

        Working...