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

前端 未结 7 1608
孤独总比滥情好
孤独总比滥情好 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: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]*/
    

提交回复
热议问题