Can't figure out why "\n" is not removed
The solution is probably an easy one, but I can't figure out why it doesn't work:
I submit a from using jQuery's Form plug-in, then run the it through:
str_replace(array("\r", "\n\t"), "", $_POST['mytext'])
Looking at the HTTP header, "\n" are not added anywhere. However, when I then print $_POST['mytext'] from PHP (just for testing purposes), Firebug shows "\n" all over the place:
<html>\n <head>\n <title></title>\n </head>\n <body>\n <h3>\n We are.....
What's interesting is that if I select this output, manually assign it to a var and then run str_replace on it, all \n characters disappear. WHY wouldn't it do with the $_POST variable?
Please help. Going crazy with what seems to be a trivial task.
P.S. I do have a .htaccess rewrite rule, but doubt it messes with anything:
RewriteRule ^/update_text/? my.php?action=update_text [QSA]
QSA is needed to transmit other $_GET data from my JS.
Problem "kind of solved". I replaced $_POST with $_REQ开发者_如何学CUEST and it's all dandy now. Still don't understand why $_POST would behave this way.
your not replacing "\n" and you might be running into a the "\r\n" problem
// Order of replacement
$str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order = array("\r\n", "\n", "\r");
str_replace($order, "", $str);
Have you tried using the PHP end of line constant? PHP_EOL
I think php automatically escapes posted data - so to get it un-escaped use stripslashes($_POST['mytext']). This behavior can be changed in the php.ini settings for magic quotes, but it is a good idea to keep it there as a safety precaution against injection attacks.
精彩评论