By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,040 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,040 IT Pros & Developers. It's quick & easy.

nl2br function and double spaces?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
"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.