cancel
Showing results for 
Search instead for 
Did you mean: 

nl2br() - won't work with variables?

7up
Community Veteran
Posts: 15,855
Thanks: 1,602
Fixes: 18
Registered: ‎01-08-2007

nl2br() - won't work with variables?

Hi
On my site I'm trying to use a <Textarea> as an input for an email form. When I send the email comes out with new lines formattred with \r\n which is rather annoying as the format isn't kept.
So.. I tried using $Message = nl2br($Message); but it doesn't work. It doesn't even strip out \r\n yet alone replace it with the br tag.
When I tried:
$Message2 = "This is\r\n a test\r\n\r\n to see if it works";
$Message2 = nl2br($Message2);
It worked flawlessly.
Why?
Here is the exact code in my script:
Print "$Message<BR>";
$Message2 = nl2br("$Message"); (Also tried without " " characters)
Print "$Message2<BR>";
$Message2 = "This is\r\n a test\r\n\r\n to see if it works";
Print "$Message2<BR>";
$Message2 = nl2br($Message2);
Print "$Message2<BR>";
I need a new signature... i'm bored of the old one!
8 REPLIES 8
7up
Community Veteran
Posts: 15,855
Thanks: 1,602
Fixes: 18
Registered: ‎01-08-2007

Re: nl2br() - won't work with variables?

just to make matters worse php seems to be randomly dropping dead or getting lazy:
$Message = str_replace("\r\n", "\n", $Message);
Doesn't work either  Angry
This is crazy. How can php decide it won't execute the same lines of code???
I need a new signature... i'm bored of the old one!
7up
Community Veteran
Posts: 15,855
Thanks: 1,602
Fixes: 18
Registered: ‎01-08-2007

Re: nl2br() - won't work with variables?

Well, after 20 views of people who obviously know what nl2br() means (or you wouldn't be reading this) I've worked something out:
str_replace() only likes single quotes not double quotes.
Unfortunately the \n still isn't being recognised by the browser and instead if being printed.
Will one of you PLEASE help me?
I need a new signature... i'm bored of the old one!
James
Grafter
Posts: 21,036
Thanks: 5
Registered: ‎04-04-2007

Re: nl2br() - won't work with variables?

Sorry - I read every post to get it out of my unread posts list.
Apologies that this isn't an answer to your query, I know rather little about coding.  Well, probably nothing at all.
7up
Community Veteran
Posts: 15,855
Thanks: 1,602
Fixes: 18
Registered: ‎01-08-2007

Re: nl2br() - won't work with variables?

You realise at the bottom of the topic list there is a link that says 'Mark Read' right? - That will do the job for you in an instant Wink
I need a new signature... i'm bored of the old one!
pierre_pierre
Grafter
Posts: 19,757
Thanks: 3
Registered: ‎30-07-2007

Re: nl2br() - won't work with variables?

wish I had done that had enough abuse already today from other people
7up
Community Veteran
Posts: 15,855
Thanks: 1,602
Fixes: 18
Registered: ‎01-08-2007

Re: nl2br() - won't work with variables?

Right... it's got something to do with the newline \n character.
If I hard code it like:
Print "This\nis\na\ntest"
the browser won't display the \n BUT the source is correctly formatted.
If I use
Print $Message;
It WILL print the \n on the actual webpage.
Why?
I need a new signature... i'm bored of the old one!
7up
Community Veteran
Posts: 15,855
Thanks: 1,602
Fixes: 18
Registered: ‎01-08-2007

Re: nl2br() - won't work with variables?

Finally... not that anyone cares despite reading...
mysql_real_escape_string is responsible. It adds an extra slash so... \r\n becomes \\r\\n.
I need a new signature... i'm bored of the old one!
Peter_Vaughan
Grafter
Posts: 14,469
Registered: ‎30-07-2007

Re: nl2br() - won't work with variables?

What does $array look like before calling stripslashes? i.e. do a print_r($array). I suspect you will only see a single slash in the data because php is using the first \ as a delimiter to ensure the second \ appears in the string. You thn call stripslashes??? which gets rid of the first and only \ and not the two \\ you thought was there.
Also what does stripslashes_deep() do as I'm not familiar with that function. If that is not a php function it may have a problem (or the data does only have one \ as detailed above). Instead of doing an array try with several variables and use stripslashes() instead of stripslashes_deep() as an experiment.
mysql_real_escape_string is used prior to writing data to a mysql database and should not be used for general string manipulation to screen as it also escapes certain other characters that can cause issues when writing to a database.
never used nl2br(), I just use mysql_real_escape_string() in a function to format the text prior to writing to the DB and just use what I get back from the DB in the textarea when displaying what the user typed in. I also strip out all \r\n and replace with a space in emails.
You also need to read up on the difference between surrounding text with a single quote or a double quote.
e.g. print '\r\nhello\r\n' will show \r\nhello\r\n
print "\r\nhello\r\n" will show <cr><lf>hello<cr><lf> where <cr> is a carriage return character and <lf> a line feed character.
in other words escaped characters (\?) are interpreted and converted within text surrounded by double quotes but left as is with text surrounded with single quotes.
This also explains your str_replace() problems...
double quote example: str_replace("\r\n", " ", $string) is looking for 2 characters <cr> and <lf> because \r is seen as a single char and \n also a single char. You could do str_replace("\r\n", '\r\n', $string) which will replace <cr><lf> with 4 characters \r\n
single quote example: str_replace('\r\n', " ", $string) is looking to match 4 characters \ r \ and n and replace all 4 with a single space. So you could do... str_replace('\r\n', "\r\n", $string) which will replace the 4 characters \r\n with 2 characters <cr><lf>.