468,296 Members | 1,328 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,296 developers. It's quick & easy.

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=nl2br($_POST["emailBody"]);
$emailBody=str_replace("<br />", "<br>", "$emailBody");

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

Now what was once single new lines is now doubled.

Any ideas?

--Matt
Jul 17 '05 #1
3 3947
On 15 Apr 2004 15:31:18 -0700, ma**@killermookie.org (Matthew Sims) wrote:
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=nl2br($_POST["emailBody"]);
$emailBody=str_replace("<br />", "<br>", "$emailBody");

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

Now what was once single new lines is now doubled.

Any ideas?


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

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2
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 <an**@andyh.co.uk> wrote in message news:<76********************************@4ax.com>. ..
On 15 Apr 2004 15:31:18 -0700, ma**@killermookie.org (Matthew Sims) wrote:
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=nl2br($_POST["emailBody"]);
$emailBody=str_replace("<br />", "<br>", "$emailBody");

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

Now what was once single new lines is now doubled.

Any ideas?


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

Jul 17 '05 #3
"Matthew Sims" <ma**@killermookie.org> wrote in message
news:1e**************************@posting.google.c om...
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! :)


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
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Marco Snoek | last post: by
3 posts views Thread by Mr Utkal Ranjan | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.