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

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

    I think regex is the way to go, and as far as I can remember the restrictions are:

    • alphanumeric
    • must start with a letter
    • can contain an underscore

    so the regex would be "/[a-zA-Z]+[0-9a-zA-Z_]*/" - off the top of my head so your milage may vary.

提交回复
热议问题