tags:

views:

38

answers:

2

I'm having to manually write:

if($pattern == 'REGEX_EMAIL') $pattern = REGEX_EMAIL;
if($pattern == 'REGEX_PASSWORD') $pattern = REGEX_PASSWORD;

Is there an other way?

+3  A: 

You could use the constant function to get the constant’s value by its name:

if (substr($pattern, 0, 6) == 'REGEX_' && defined($pattern)) {
    $pattern = constant($pattern);
}
Gumbo
+1  A: 

One solution (albeit ridiculous) would be to use an associative array...

$regexs = array('REGEX_NAME' => REGEX_NAME, 'REGEX_EMAIL' => REGEX_EMAIL);
clownbaby
+1 This is a good solution if you want to restrict the set of *REGEX\_…* constants.
Gumbo