5.11 Checking the Syntax of a Regular Expression


You want to let users use their own patterns or you just need to check your own regular expressions.

Technique

Either set the error_reporting level in your php.ini file or use the error_reporting function to set an E_WARNING level of tolerance:

 <?php error_reporting(E_WARNING); $line = "maray"; ereg("(marray", $line, $ar); // Invalid Regex ?> 

Comments

The error_reporting() function is very useful for detecting blatant errors in your regular expressions such as the error here (a missing closing parenthesis). However, do note that error_reporting() is not a complete syntax checker, so your code is still subject to many mistakes and exploits.

The error_reporting() function should be used purely for debugging rather than for validating users' regular expressions. It does not check whether the regular expression makes sense, but rather whether the regular expression displays valid syntax. When you create programs, you should always set error_reporting() to the most stringent level possible ( E_ALL ). Then when the program is done with testing, you can lower the level of error_reporting() .



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net