Jump to content


This topic is now archived and is closed to further replies.


newlines, textarea POSTs and JavaScript

Recommended Posts

nl2br() is great for formatting text for HTML output, but it does not actually remove the newlines. 

I sometimes need to remove newlines entirely from textarea POSTs (to use as values for JavaScript variables), but the str_replace and related functions do not detect newlines from POSTs using the escaped '\n' or '\r\n'

For example, I have found that newlines entered into textareas and submitted via POST cannot be detected with
    strpos($mystring, '\r\n'); or strpos($mystring, '\n');

but if I use the following code:
    strpos($mystring, $newline);

the newlines are detected!

How can these newlines be detected without actually explicitly defining a newline?

Share this post

Link to post
Share on other sites
You are using single quotes around the newline characters. PHP will treat \n or \r as-is, it will not treat them as newline/carraige return characters. You'll want to use double quotes like so:
[code]$text = "This has \n new line \r chars, \n and stuff";

$text = str_replace(array("\r", "\n"), '', $text);

echo '<pre>' . $text . '</pre>';[/code]

Share this post

Link to post
Share on other sites
Thanks for your reply.  Unfortunately, it did not help me. 

Let's say I have a textarea as follows submitted with a carriage return in it-
<textarea name="text"></textarea>

When receiving the POST, I want to identify and replace the carriage returns entered into the form. The following code does not work:

str_replace("\r", '', $text);, str_replace("\n", '', $text);,
str_replace("\r\n", '', $text);

The above functions do not remove the newlines.  If instead I use

as the string to replace, it works!

Share this post

Link to post
Share on other sites
I looked into this again, and, alas, I was mistaken.  I had looked at the problem so much, I thought I had tried something I didn't!  I went back, and thank you wildteen, you are correct.  I was not aware of any differences between single and double quotes in PHP (or JavaScript for that matter).  Are there are others I should know about?

Share this post

Link to post
Share on other sites
Another difference between single and double quotes, is variables will not be parsed within single quotes, they will be treated as normal text. For example you have a variable called $foo which holds a string - bar. So when you go to echo it out with single quotes:
[code]$foo = 'bar';
echo 'Hey I went to the $foo';[/code]
You'll get this result:
[i]Hey I went to the $foo[/i]
As you can see $foo hasnt been repleaced with its value when using single quotes. However if you use double quotes it'll parse the variable:
[code]$foo = 'bar';
echo "Hey I went to the $foo";[/code]
Which will now give you this result:
[i]Hey I went to the bar[/i]

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.