如何让PHP在查询字符串中忽略未转义的符号(例如,name=M&M's不变成数组('name' => 'M', 'M\'s' => '))

时间:2023-01-13 11:19:41

With a URL like: http://any.php?name=M&M's

有一个URL: http://any.php?name=M&M's。

PHP's $_GET variable is array('name' => 'M', 'M\s' => '').

PHP的$ _GET数组变量('名字' = > ' M ',' M \ s ' = >”)。

Is there a way to get PHP to ignore that un-escaped ampersand?

有没有一种方法可以让PHP忽略非转义的符号?

------ Earlier question (that lead to one above) ------

------ ------ ------ ------ ------ ----。

Hi guys,

嗨,伙计们,

With a mod_rewrite rule like this...

使用mod_rewrite规则,像这样…

RewriteRule ^wiki/([A-Za-z0-9_,+&'\-\)\(]+)$ php/data.php?name=$1 [L,NC]

[RewriteRule ^ wiki /(- za - z0 - 9 _,+ & ' \ \)\]+)php / data.php美元?name = 1美元(L,数控)

And a link like this (in plain text, that's M&M's)...

像这样的链接(在纯文本中,这是M&M的)…

http://any.com/wiki/M%26M%27s

http://any.com/wiki/M%26M%27s

I'm getting this from data.php...

我从data.php中得到这个…

<?php echo 'Name:'. $_GET['name']; ?>

< ?php echo '名称:'。$ _GET['名字'];? >

Name: M

名称:米

What happened to the rest of the name?

剩下的名字怎么了?

Thanks!

谢谢!

2 个解决方案

#1


0  

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:

我不知道这是否有帮助,但是你确定你需要在范围表达式中的反斜杠吗?通常情况下,你不需要在那里转义(它们永远都不合法),而且你也无法逃脱连字符:你要么在第一,要么在最后一个位置。所以直线应该是:

RewriteRule ^wiki/([-A-Za-z0-9_,+&')(]+)$ php/data.php?name=$1 [L,NC]

And, again, I don't know mod_rewrite, so I'm just taking two stabs in the dark here but:

再一次,我不知道mod_rewrite,所以我只是在这里用了两个刺,但是

  1. Is the $ just before "php" part of the regex, or some sort of variable reference? Is there confusion?
  2. 是在regex的“php”部分之前的$,还是某种变量引用?有困惑吗?
  3. Are you sure this rule should be operating on the de-escaped version of the URL? If it's being passed a string with %'s in it, then your regex should have %'s in it...no?
  4. 您确定这个规则应该在URL的脱逃版本上运行吗?如果它在其中传递了一个%'s的字符串,那么你的正则表达式应该有%'s,不是吗?

#2


0  

So I did a little more testing, and it looks like PHP's fault!

所以我做了更多的测试,看起来就像PHP的错误!

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).

首先,在运行重写规则之前,mod_rewrite会对任何字符进行解码。如果我从regex的字符类中删除“$”和“'”,我就会得到一个404错误。因此,我的RewriteRule的语法和mod_rewrite看起来越来越好(特别是在像Steve建议的那样清理之后)。

If I dump the page's $_GET variable, I see:

如果我转储页面的$_GET变量,我看到:

<?php var_dump($_GET); ?>
array(2) { ["name"]=> string(8) "Almond_M" ["M\'s"]=> string(0) "" } 

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).

哈!这是一个PHP的问题!现在的问题是,如何让PHP忽略未转义的符号(并且我想要保留它,以使它看起来更适合用户)。

#1


0  

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:

我不知道这是否有帮助,但是你确定你需要在范围表达式中的反斜杠吗?通常情况下,你不需要在那里转义(它们永远都不合法),而且你也无法逃脱连字符:你要么在第一,要么在最后一个位置。所以直线应该是:

RewriteRule ^wiki/([-A-Za-z0-9_,+&')(]+)$ php/data.php?name=$1 [L,NC]

And, again, I don't know mod_rewrite, so I'm just taking two stabs in the dark here but:

再一次,我不知道mod_rewrite,所以我只是在这里用了两个刺,但是

  1. Is the $ just before "php" part of the regex, or some sort of variable reference? Is there confusion?
  2. 是在regex的“php”部分之前的$,还是某种变量引用?有困惑吗?
  3. Are you sure this rule should be operating on the de-escaped version of the URL? If it's being passed a string with %'s in it, then your regex should have %'s in it...no?
  4. 您确定这个规则应该在URL的脱逃版本上运行吗?如果它在其中传递了一个%'s的字符串,那么你的正则表达式应该有%'s,不是吗?

#2


0  

So I did a little more testing, and it looks like PHP's fault!

所以我做了更多的测试,看起来就像PHP的错误!

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).

首先,在运行重写规则之前,mod_rewrite会对任何字符进行解码。如果我从regex的字符类中删除“$”和“'”,我就会得到一个404错误。因此,我的RewriteRule的语法和mod_rewrite看起来越来越好(特别是在像Steve建议的那样清理之后)。

If I dump the page's $_GET variable, I see:

如果我转储页面的$_GET变量,我看到:

<?php var_dump($_GET); ?>
array(2) { ["name"]=> string(8) "Almond_M" ["M\'s"]=> string(0) "" } 

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).

哈!这是一个PHP的问题!现在的问题是,如何让PHP忽略未转义的符号(并且我想要保留它,以使它看起来更适合用户)。