Regular Expressions: RegEx for determining valid PHP class property names?

Posted by Brian Lacy on Stack Overflow See other posts from Stack Overflow or by Brian Lacy
Published on 2010-03-25T22:33:00Z Indexed on 2010/03/25 22:43 UTC
Read the original article Hit count: 370

Filed under:
|
|
|

I am using PHP's magic __set and __get methods to access a private array in a class. I want to make sure the property names requested (i.e. $myObj->FakeProperty) are valid according to PHP property name rules before accessing them.

My current RegEx isn't doing the trick; with my test values, _12 always falls through the cracks.

I'm not actually sure that my test values even represent a realistic representation of what is and isn't allowed for PHP class property names, but I'm not really too concerned about it, just that I have some sort of rudimentary check in place.

Test Fields:

albert12
12Albert
_12
_Albert12
_12Albert
_____a_1

RegEx:

^(?=_*[A-z]+)[A-z0-9_]+$

© Stack Overflow or respective owner

Related posts about php

Related posts about regex