[thelist] How to tell if a regular expression is valid?

sbeam sbeam at syxyz.net
Thu Jul 15 08:43:40 CDT 2004


On Wednesday 14 July 2004 10:03 pm, Joshua Olson wrote:
> > That would work, if I could try/catch in php4... php5 might have it
> > but I don't have that option at this point...

you could use eval() maybe, though try/catch would be much easier...
http://us4.php.net/eval

something like 
$re = preg_replace("/(\/')/", '\\$1', $users_re); # escape ' $ and / 
$res = @eval("return preg_match('/$re/', \$test);");
print $res;

here you have the use of both @ and eval() which normally I am against. 
But this might be one of the few times they are called for...

Also, you could use this feature
http://us3.php.net/manual/en/reserved.variables.php#reserved.variables.phperrormsg
to determine if your eval() returned false because of a compile failure 
or just a non-match...

good luck ;)

-- 

# S Beam - Web App Dev Servs
# http://www.onsetcorps.net/


More information about the thelist mailing list