Jump to content

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

the newlines are detected!

How can these newlines be detected without actually explicitly defining a newline?
Link to comment
https://forums.phpfreaks.com/topic/15729-newlines-textarea-posts-and-javascript/
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]
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!

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?
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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.