Results 1 to 13 of 13

Thread: nl2br

  1. #1

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444

    nl2br

    Everytime I use nl2br() it seems to replace every new lines with 2 <br /> tags. Does this happen to everyone or is it just me?!

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Why not?:

    Code:
    $string = str_replace('\n', '<br>\n', $string);
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    I have never had that problem. What version of PHP are you running?

    Also a more correct statement to hobo's response is the following:
    $string = preg_replace("/(\n\r|\r|\n)/isu","<br />",$string);
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  4. #4

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Alright, thanks for the responses. I have already tried what you said Hobo and it seems to do the same things. I'll try cpradio's suggestion today, but I assume it's something to do with my code as no-one else has the problem. And my host is using version 4.2.2, and the same thing happened when they had an earlier version.

  5. #5

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I tested cpradio's sugesstion and it was the same, but I'm using the following as a workaround, if anyone has any better ideas let me know:

    Code:
    $string = preg_replace("/(<br \/><br \/>)/isu","<br />", preg_replace("/(\n\r|\r|\n)/isu","<br />",$string));

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    you can try it this way

    $string= ereg_replace("(\r\n|\n|\r)", "<br />", $string);


    no need to have isu in there.

  7. #7
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    true, the isu isnt needed, i just didnt want to take it out of the template I pulled it from, guess im just too lazy
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  8. #8

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Thanks, I did figure I could take it out. My friend suggested that I should turn all \r\n into newlines to fix windows, and replace \r with \n to fix Macs then use nl2br, so I'll try that (when my servers stops being a cry baby and starts working again )

  9. #9
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    The code that I and phpman posted does just that. It converts windows line endings to <br /> and Macs to <br />
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  10. #10

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Oh yeah right, why didn't I notice that?

  11. #11
    Lively Member
    Join Date
    Sep 2002
    Location
    Newfoundland
    Posts
    71
    My host's PHP also does the double <br> with nl2br() as well. Thanks for the workaround, its really useful!
    - Adam Lundrigan

  12. #12

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    So at least I'm not alone Must be a dodgy setup or something.

  13. #13

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I think I figured out why I was getting to <br />s. It was because I had an implode("\n", $string), then I was using nltbr on it. I think I should have had an empty string first the glue/first argument. Would that have been the cause?

Posting Permissions

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



Click Here to Expand Forum to Full Width