我正在编写表单验证类,并希望在验证中包含正则表达式。因此,提供的正则表达式不保证有效。
我怎样(有效地)检查正则表达式是否有效?
我正在编写表单验证类,并希望在验证中包含正则表达式。因此,提供的正则表达式不保证有效。
我怎样(有效地)检查正则表达式是否有效?
使用你的模式 preg_*
调用。如果函数返回 false
您的模式可能存在问题。据我所知,这是检查正则表达式模式在PHP中是否有效的最简单方法。
这是一个指定正确类型的布尔检查的示例:
$invalidPattern = 'i am not valid regex';
$subject = 'This is some text I am searching in';
if (@preg_match($invalidPattern, $subject) === false) {
// the regex failed and is likely invalid
}
使用你的模式 preg_*
调用。如果函数返回 false
您的模式可能存在问题。据我所知,这是检查正则表达式模式在PHP中是否有效的最简单方法。
这是一个指定正确类型的布尔检查的示例:
$invalidPattern = 'i am not valid regex';
$subject = 'This is some text I am searching in';
if (@preg_match($invalidPattern, $subject) === false) {
// the regex failed and is likely invalid
}
您不应该使用@来消除所有错误,因为它也会使致命错误无声。
function isRegularExpression($string) {
set_error_handler(function() {}, E_WARNING);
$isRegularExpression = preg_match($string, "") !== FALSE;
restore_error_handler();
return isRegularExpression;
}
这只会使preg_match调用的警告静音。
当您有错误报告时,您无法简单地测试布尔结果。如果正则表达式失败则抛出警告(即'警告:未找到结束分隔符xxx'。)
我觉得奇怪的是,PHP文档没有告诉我们这些抛出的警告。
下面是我使用try,catch解决这个问题的方法。
//Enable all errors to be reported. E_WARNING is what we must catch, but I like to have all errors reported, always.
error_reporting(E_ALL);
ini_set('display_errors', 1);
//My error handler for handling exceptions.
set_error_handler(function($severity, $message, $file, $line)
{
if(!(error_reporting() & $severity))
{
return;
}
throw new ErrorException($message, $severity, $severity, $file, $line);
});
//Very long function name for example purpose.
function checkRegexOkWithoutNoticesOrExceptions($test)
{
try
{
preg_match($test, '');
return true;
}
catch(Exception $e)
{
return false;
}
}
如果表达式出现问题,这是我使用即将发出警告的解决方案:
function isRegEx($test)
{
$notThisLine = error_get_last();
$notThisLine = isset($notThisLine['line']) ? $notThisLine['line'] + 0 : 0;
while (($lines = rand(1, 100)) == $notThisLine);
eval(
str_repeat("\n", $lines) .
'@preg_match(\'' . addslashes($test) . '\', \'\');'
);
$check = error_get_last();
$check = isset($check['line']) ? $check['line'] + 0 : 0;
return $check == $notThisLine;
}