Jump to content


Photo

newlines, textarea POSTs and JavaScript


  • Please log in to reply
4 replies to this topic

#1 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 July 2006 - 06:38 PM

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:
    $newline='
';
    strpos($mystring, $newline);

the newlines are detected!

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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 07:12 PM

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:
$text = "This has \n new line \r chars, \n and stuff";

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

echo '<pre>' . $text . '</pre>';


#3 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 26 July 2006 - 08:24 PM

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:

$text=$_POST['text'];
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
$newline="
";

as the string to replace, it works!



#4 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 27 July 2006 - 02:26 AM

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?

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 July 2006 - 09:55 AM

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:
$foo = 'bar';
echo 'Hey I went to the $foo';
You'll get this result:
Hey I went to the $foo
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:
$foo = 'bar';
echo "Hey I went to the $foo";
Which will now give you this result:
Hey I went to the bar




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users