bltesar Posted July 26, 2006 Share Posted July 26, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/15729-newlines-textarea-posts-and-javascript/ Share on other sites More sharing options...
wildteen88 Posted July 26, 2006 Share Posted July 26, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/15729-newlines-textarea-posts-and-javascript/#findComment-64278 Share on other sites More sharing options...
bltesar Posted July 26, 2006 Author Share Posted July 26, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/15729-newlines-textarea-posts-and-javascript/#findComment-64303 Share on other sites More sharing options...
bltesar Posted July 27, 2006 Author Share Posted July 27, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/15729-newlines-textarea-posts-and-javascript/#findComment-64458 Share on other sites More sharing options...
wildteen88 Posted July 27, 2006 Share Posted July 27, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/15729-newlines-textarea-posts-and-javascript/#findComment-64564 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.