如何让 PHP 忽略查询字符串中未转义的 & 符号(例如 ?name=M&M's 不会成为数组('name' => 'M' ;, 'M\'s'))
URL 如下: http://any.php?name=M&M's
PHP 的 $_GET
变量是 array('name' => 'M ', 'M\s' => '')
。
有没有办法让 PHP 忽略那个未转义的 & 符号?
------ 之前的问题(导致上面的问题) ------
大家好,
有了这样的 mod_rewrite 规则...
RewriteRule ^wiki/([A -Za-z0-9_,+&'\-\)\(]+)$ php/data.php?name=$1 [L,NC]
和这样的链接(纯文本形式,即M&M's)...
http://any.com/wiki/M%26M%27s
我从 data.php 得到这个...
名称:M
名称的其余部分发生了什么?
谢谢!
With a URL like: http://any.php?name=M&M's
PHP's $_GET
variable is array('name' => 'M', 'M\s' => '')
.
Is there a way to get PHP to ignore that un-escaped ampersand?
------ Earlier question (that lead to one above) ------
Hi guys,
With a mod_rewrite rule like this...
RewriteRule ^wiki/([A-Za-z0-9_,+&'\-\)\(]+)$ php/data.php?name=$1 [L,NC]
And a link like this (in plain text, that's M&M's)...
http://any.com/wiki/M%26M%27s
I'm getting this from data.php...
<?php echo 'Name:'. $_GET['name']; ?>
Name: M
What happened to the rest of the name?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否有帮助,但是您确定需要在范围表达式中使用反斜杠吗?通常,您不需要在那里转义括号(它们永远不会合法),并且您也不需要转义连字符:您将其包含在第一个或最后一个位置。所以你的行将是:
而且,再说一遍,我不知道 mod_rewrite,所以我只是在黑暗中进行了两次尝试,但是:
I don't know if this helps, but are you sure you need the backslashes in the range expression? Normally you don't need to escape parentheses there (they would never be legal), and you don't escape the hyphen either: you include it either in first or last position. So your line would be:
And, again, I don't know mod_rewrite, so I'm just taking two stabs in the dark here but:
所以我又做了一点测试,看来是 PHP 的错!
首先,mod_rewrite 在运行重写规则之前似乎会解码任何字符。如果我从正则表达式的字符类中删除“$”和“'”(或按照 Steve 建议添加“%”),则会收到 404 错误。因此,我的 RewriteRule 语法和 mod_rewrite 看起来越来越好(特别是在像 Steve 建议的那样清理之后)。
如果我转储页面的
$_GET
变量,我会看到:哈!这是一个 PHP 问题!所以现在的问题是,如何让 PHP 忽略未转义的 & 符号(我想保持它不转义,这样对用户来说看起来很漂亮)。
So I did a little more testing, and it looks like PHP's fault!
First of all, it appears mod_rewrite decodes any characters before running through the re-write rules. If I remove the "$" and "'" from my regex's character class (or add the "%" as Steve suggested), I get a 404 error. Thus, my RewriteRule's syntax and mod_rewrite looks better and better (especially after cleaning it up like Steve suggested).
If I dump the page's
$_GET
variable, I see:Ha! It's a PHP problem! So now the question is, how do I get PHP to ignore the unescaped ampersand (and I'd like to keep it un-escaped so it looks pretty for users).