473,245 Members | 1,749 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,245 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 4246
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.