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

前端 未结 7 1579
孤独总比滥情好
孤独总比滥情好 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: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
    }
    

提交回复
热议问题