dot in variable name

前端 未结 3 1998
梦如初夏
梦如初夏 2020-12-18 00:47

I am passing the variable with dot in query string.Php is replacing the dot with under score. So how can i retain the variable name which is having dot in the name

h

相关标签:
3条回答
  • 2020-12-18 01:04

    Use $_SERVER['QUERY_STRING']

    $get = array();
    foreach(explode('&', $_SERVER['QUERY_STRING']) as $part)
        {
        $part = explode('=', $part);
        if($key = array_shift($part))
            {
            $get[ $key ] = implode('', $part);
            }
        }
    print_r($get);
    

    Result for your example Array ( [one.txt] => on [two.txt] => on )

    0 讨论(0)
  • 2020-12-18 01:10

    The PHP developers implemented this in order to support register_globals() but if they'd paused for more than one second to consider the consequences then they would have only altered the names imported to the global variable namespace, not in $_POST itself. There's less than no reason for altering the request variables themselves... well, other than to make PHP incapable of handling standard form submissions.

    Here's the solution for POST variables as well, which is probably trickier for more users than the GET solution:

    function post_data(){    
       $data=explode('&',file_get_contents("php://input"));
       $post=array();
       foreach ($data as $var){
          list($key,$value)=explode('=',$var,2);
          $post[$key]=urldecode($value);
       }
       return $post; 
    }
    
    0 讨论(0)
  • 2020-12-18 01:15

    The reason PHP is converting your variable name from one.txt into one_txt is because dots are not valid in variable names.

    For more details, look at the PHP Documentation:

    Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

    You can either account for the change (. to _) and check for $_REQUEST['one_txt'] or you can make your HTML form pass a valid variable name instead.

    Edit:

    To follow-up on Michael Borgwardt's comment, here's the text from PHP's documentation about handling variables from external sources:

    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.

    It is indeed a PHP specific thing.

    0 讨论(0)
提交回复
热议问题