How to check if a string can be used as a variable name in PHP?

前端 未结 7 1602
孤独总比滥情好
孤独总比滥情好 2021-02-15 18:08

In PHP one can use variable variables...

For example...

class obj { }
$fieldName = \"Surname\";
$object = new obj();
$object->Name = \"John\";
$objec         


        
相关标签:
7条回答
  • 2021-02-15 18:35

    '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' will validate a PHP variable name.

    This regex is directly from the documentation at: http://www.php.net/manual/en/language.variables.basics.php

    0 讨论(0)
  • 2021-02-15 18:36

    As it was already replied, but not with a complete line of code:

    if ( preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/','evaluation string') )
    {
        // the string is valid
    }
    else
    {
        // the string is not valid
    }
    
    0 讨论(0)
  • 2021-02-15 18:37

    Validating with RegEx if you wanted to allow $ or &$ (pass variable by reference) to be validated in the string, you could use this regex:

    /^([\$]|(&\$))[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
    
    0 讨论(0)
  • 2021-02-15 18:42

    From the manual:

    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]*'

    So If you ran your string through the RegEx, you should be able to tell if it's valid or not.

    It should be noted that the ability to access 'invalid' Object property names using a variable variable is the correct approach for some XML parsing.

    For example, from the SimpleXML docs:

    Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

    Followed by this code example:

    echo $xml->movie->{'great-lines'}->line;
    

    So it's not necessarily wrong to have properties that can only be accessed this way.

    However, if your code both creates and uses the object - one would wonder why you would use those kind of properties. Allowing, of course, a situation similar to the SimpleXML example, where an object is created to represent something outside the scope of your control.

    0 讨论(0)
  • 2021-02-15 18:42

    However, `$fieldName string may contain some characters not allowed in variable names. PHP will still create the field with that name (much like the associative array), but I will not be able to access it with $object->...... because it would not parse correctly.

    You will still be able to access the field through the $object->{"fieldname"} syntax.

    As far as I know, the only restriction is that you can't access properties with \x00 in the name and you can't define variables starting with \x00.

    Example:

    $a = new stdclass;
    $a->{"\x00dd"} = 8; //invalid
    $a->{"dd\x00dd"} = 8; //valid, but...
    echo $a->{"dd\x00dd"}; //can't read (no property "dd")
    
    0 讨论(0)
  • 2021-02-15 18:42

    but I will not be able to access it with $object->...... because it would not parse correctly

    but look:

    class A {}
    
    $varName = '!asd asd';
    $a = new A();
    $a->$varName = '1';
    echo "{$a->{'!asd asd'}}"; // 1
    

    Certainly not recommended but it can be done.

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