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?! :confused:
Printable View
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?! :confused:
Why not?:
Code:$string = str_replace('\n', '<br>\n', $string);
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);
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.
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));
you can try it this way
$string= ereg_replace("(\r\n|\n|\r)", "<br />", $string);
no need to have isu in there.
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 :(
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 ;))
The code that I and phpman posted does just that. It converts windows line endings to <br /> and Macs to <br />
Oh yeah right, why didn't I notice that? :o
My host's PHP also does the double <br> with nl2br() as well. Thanks for the workaround, its really useful!
So at least I'm not alone :D Must be a dodgy setup or something.
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?