让 PHP 停止替换 '.'$_GET 或 $_POST 数组中的字符? [英] Get PHP to stop replacing '.' characters in $_GET or $_POST arrays?

查看:15
本文介绍了让 PHP 停止替换 '.'$_GET 或 $_POST 数组中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我通过 $_GET 在名称中传递带有 . 的 PHP 变量,PHP 会自动将它们替换为 _ 字符.例如:

... 输出以下内容:

url 是/SpShipTool/php/testGetUrl.php?x.y=a.bx.y 是 .x_y 是 a.b.

...我的问题是:有没有任何方法可以阻止这种情况?终其一生都无法弄清楚我做了什么值得这个

我运行的 PHP 版本是 5.2.4-2ubuntu5.3.

解决方案

PHP.net 对此的解释如下:

<块引用>

传入变量名称中的点

通常,PHP 不会改变变量名传递到脚本中.然而,它需要注意的是点(句号,句号)不是有效字符PHP 变量名.由于这个原因,看看吧:

现在,什么解析器看到的是一个名为的变量$varname,后跟字符串连接运算符,后跟裸字符串(即未加引号的字符串不匹配任何已知密钥或保留字)'ext'.显然,这没有达到预期的结果.

因此,重要的是注意 PHP 会自动替换传入变量中的任何点带下划线的名称.

来自 http://ca.php.net/variables.external.>

此外,根据此评论这些其他字符被转换为下划线:

<块引用>

PHP 转换为 _(下划线)的字段名称字符的完整列表如下(不仅仅是点):

  • chr(32) ( )(空格)
  • chr(46) (.)(点)
  • chr(91) ([)(左方括号)
  • chr(128) - chr(159)(各种)

所以看起来你被它卡住了,所以你必须使用 dawnerd 的建议(我只是使用 str_replace.)

If I pass PHP variables with . in their names via $_GET PHP auto-replaces them with _ characters. For example:

<?php
echo "url is ".$_SERVER['REQUEST_URI']."<p>";
echo "x.y is ".$_GET['x.y'].".<p>";
echo "x_y is ".$_GET['x_y'].".<p>";

... outputs the following:

url is /SpShipTool/php/testGetUrl.php?x.y=a.b
x.y is .
x_y is a.b.

... my question is this: is there any way I can get this to stop? Cannot for the life of me figure out what I've done to deserve this

PHP version I'm running with is 5.2.4-2ubuntu5.3.

解决方案

Here's PHP.net's explanation of why it does it:

Dots in incoming variable names

Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it:

<?php
$varname.ext;  /* invalid variable name */
?>

Now, what the parser sees is a variable named $varname, followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result.

For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores.

That's from http://ca.php.net/variables.external.

Also, according to this comment these other characters are converted to underscores:

The full list of field-name characters that PHP converts to _ (underscore) is the following (not just dot):

  • chr(32) ( ) (space)
  • chr(46) (.) (dot)
  • chr(91) ([) (open square bracket)
  • chr(128) - chr(159) (various)

So it looks like you're stuck with it, so you'll have to convert the underscores back to dots in your script using dawnerd's suggestion (I'd just use str_replace though.)

这篇关于让 PHP 停止替换 '.'$_GET 或 $_POST 数组中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆