473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 4258
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marco Snoek | last post by:
Hi, Hope you can help me out with this one.. nl2br($string) converts all the \n to <br \> ... but .. Suppose I have a table or other HTML elements in $string.. All the TD> and TR> and...
3
by: Gary Besta | last post by:
I am trying to add a simple case statement to a stored procedure or user defined function. However when I try and save the function/procedure I get 2 syntax errors. Running the query in query...
3
by: Mr Utkal Ranjan | last post by:
Hi Friends I want to launch notepad with a specific file on a command click event. So I was using the "Shell" function from VB on a command click event.For ex: Shell "Notepad.exe C:\Program...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
8
by: Mike | last post by:
Am trying to open a Microsoft Word .doc file using Access 2000 with Shell function (on Windows XP Operating system) Here is the code : Shell "C:\Program Files\Microsoft...
2
Ajm113
by: Ajm113 | last post by:
Ok, I want to disable any html tags, but the problem is when I do add in the nl2br function with a htmlentities it displays the tags for the <br>! I even keep seeing rn every time I enter a return in...
2
by: Gozil | last post by:
Hello, was just wondering if its possible to have a function like nl2br in javascript. I need it to change the row where the user have used enter.
2
by: sitko | last post by:
Hi, I'm in the process of converting a VB.net program into a C program so it can run on a unix like machine. I've been moving along at a nice pace, but this conversion has stumped me. I need...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.