Turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
Showing results for
nl2br() - won't work with variables?
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Other forums
- :
- Tech Help - Software/Hardware etc
- :
- Re: nl2br() - won't work with variables?
nl2br() - won't work with variables?
09-01-2009 2:50 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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>";
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
Re: nl2br() - won't work with variables?
09-01-2009 4:01 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
This is crazy. How can php decide it won't execute the same lines of code???
$Message = str_replace("\r\n", "\n", $Message);
Doesn't work either
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!
Re: nl2br() - won't work with variables?
09-01-2009 5:09 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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?
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!
Re: nl2br() - won't work with variables?
09-01-2009 5:11 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Apologies that this isn't an answer to your query, I know rather little about coding. Well, probably nothing at all.
Re: nl2br() - won't work with variables?
09-01-2009 5:18 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
I need a new signature... i'm bored of the old one!
Re: nl2br() - won't work with variables?
09-01-2009 5:21 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
wish I had done that had enough abuse already today from other people
Re: nl2br() - won't work with variables?
09-01-2009 5:41 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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?
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!
Re: nl2br() - won't work with variables?
09-01-2009 6:06 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Finally... not that anyone cares despite reading...
mysql_real_escape_string is responsible. It adds an extra slash so... \r\n becomes \\r\\n.
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!
Re: nl2br() - won't work with variables?
09-01-2009 6:45 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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>.
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>.
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Other forums
- :
- Tech Help - Software/Hardware etc
- :
- Re: nl2br() - won't work with variables?