在PHP中,您可以通过注册自己的错误处理程序轻松检查正则表达式编译错误。我编写了一个PHP正则表达式测试器,它可以检测到正则表达式编译错误,并向用户报告这些错误,而不显示敏感信息,例如文件名和行号。关键部分在于自定义错误处理程序可以捕获正则表达式编译错误。您不必抛出异常或捕获异常。其余部分是方便的演示。
我使用set_error_handler()注册一个函数来抛出ErrorException。当运行preg_match()时,我捕获异常,然后使用跟踪信息验证异常是否从与我运行preg_match()相同的文件、行和函数抛出(如果不是,则表示由其他原因引起了错误或异常,例如在同一行中调用了不同的函数或PHP内存不足)。然后,我仅向用户输出错误消息。
请注意,此脚本实际上使用变量函数运行正则表达式函数。单独输入输入、没有前导和尾随斜杠的正则表达式以及任何修饰符。
以下是完整代码:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style type="text/css">
body {
font-family: monospace;
}
input[type=text], textarea {
letter-spacing: .25em;
font-weight: bold;
font-size: larger;
}
textarea {
width: 100%;
height: 25%;
}
fieldset {
display: inline;
}
.error {
color: red;
}
</style>
</head>
<body onload="document.getElementById('patterninput').focus();">
<?php
function testRegexErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
$pattern = isset($_REQUEST["pattern"]) ? $_REQUEST["pattern"] : "";
$mods = isset($_REQUEST["mods"]) ? $_REQUEST["mods"] : "";
$input = isset($_REQUEST["input"]) ? $_REQUEST["input"] : "";
$regex = "/$pattern/$mods";
$fns = array("match" => "preg_match", "matchall" => "preg_match_all");
$fnKey = isset($_REQUEST["function"]) ? $_REQUEST["function"] : "matchall";
$fn = isset($fns[$fnKey]) ? $fns[$fnKey] : "preg_match_all";
try {
set_error_handler("testRegexErrorHandler");
$result = $fn($regex, $input, $matches);
} catch (Exception $ex) {
}
restore_error_handler();
?>
<form action="" method="post">
<input type="text" size="100" id="patterninput" name="pattern" value="<?php echo htmlspecialchars($pattern); ?>" placeholder="Pattern" />
<input type="text" size="10" name="mods" value="<?php echo htmlspecialchars($mods); ?>" placeholder="Modifiers" />
<fieldset><legend>Function</legend>
<label for="fnmatch">preg_match()</label><input type="radio" name="function" value="match" id="fnmatch" <?php echo $fnKey == "match" ? "checked" : ""; ?> />
<label for="fnmatchall">preg_match_all()</label><input type="radio" name="function" value="matchall" id="fnmatchall" <?php echo $fnKey == "matchall" ? "checked" : ""; ?> />
</fieldset>
<input type="submit" name="submit" />
<textarea name="input" rows="10" placeholder="Input"><?php echo htmlspecialchars($input); ?></textarea>
</form>
<br/>
<?php
if(isset($ex)) {
$trace = $ex->getTrace();
if(is_array($trace) && isset($trace[1]) && is_array($trace[1])) {
$errFn = isset($trace[1]["function"]) ? $trace[1]["function"] : "";
$errLine = isset($trace[1]["line"]) ? $trace[1]["line"] : "";
$errFile = isset($trace[1]["file"]) ? $trace[1]["file"] : "";
if($errFn != "" && $errFn == $fn && $errLine != "" && $errLine == $ex->getLine() && $errFile != "" && $errFile == $ex->getFile() && get_class($ex) == "ErrorException") {
$regexErr = true;
}
}
if(empty($regexErr)) {
throw $ex;
} else {
echo "<p class=\"error\">The following error has occurred and is probably an error in your regex syntax:<br/>" .htmlspecialchars($ex->getMessage()) ."</p>\n\n";
}
}
if(isset($_REQUEST["submit"]) && isset($result)) {
echo "Result: $result<br/>\n";
echo "Matches:<pre>" .htmlspecialchars(print_r($matches, true)) ."</pre>\n\n";
}
?>
</body>
</html>