Mastering Regular Expressions
Listings from page 474
Download all listings shown below.
Chapter 10; page 474 (download)
Compilation failed: nothing to repeat at offset 0 |
Chapter 10; page 474 (download)
/*
* Return an error message if the given pattern argument or its underlying regular expression
* are not syntactically valid. Otherwise (if they are valid), false is returned.
*/
function preg_pattern_error($pattern)
{
/*
* To tell if the pattern has errors, we simply try to use it.
* To detect and capture the error is not so simple, especially if we want to be sociable and not
* tramp on global state (e.g., the value of $php_errormsg). So, if 'track_errors' is on, we preserve
* the $php_errormsg value and restore it later. If 'track_errors' is not on, we turn it on (because
* we need it) but turn it off when we're done.
*/
if ($old_track = ini_get("track_errors"))
$old_message = isset($php_errormsg) ? $php_errormsg : false;
else
ini_set('track_errors', 1);
/* We're now sure that track_errors is on. */
unset($php_errormsg);
@ preg_match($pattern, ""); /* actually give the pattern a try! */
$return_value = isset($php_errormsg) ? $php_errormsg : false;
/* We've now captured what we need; restore global state to what it was. */
if ($old_track)
$php_errormsg = isset($old_message) ? $old_message : false;
else
ini_set('track_errors', 0);
return $return_value;
} |
Fetch additional Third-Edition listings and data: