捕获正则表达式编译错误

7

我正在尝试设置一个类似于rubular的服务,但使用PHP语言和preg函数族。它将获取一个输入的正则表达式、一个测试字符串,并运行preg_match()

我该如何找出是否发生了编译错误(例如:无效的正则表达式),如果是这种情况,错误是什么?通常它会抛出警告,比如:

Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset x in ****** on line y

pcre_last_error() 在这里完全无用,因为如果正则表达式编译失败,它将返回 0(PREG_NO_ERROR)。

我正在考虑的一个选项是使用输出缓冲来捕获警告,但肯定有更好的方法。

2个回答

2

你最好的做法是使用@省略错误信息,检查返回值,并且如果为false,调用error_get_last

你也可以编写自己的包装器来封装pcre_compile。它接收存储错误代码和字符串的指针。这应该不太困难;preg_match只是一个轻量级的包装器。


0

在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
        // Translate old-style PHP errors to OO approach
        // http://www.php.net/manual/en/class.errorexception.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) {
            // $ex is used later
        }
        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";
    }
}

// result will be unset if error or exception thrown by regex function
//  such as if expression is syntactically invalid
if(isset($_REQUEST["submit"]) && isset($result)) {
    echo "Result: $result<br/>\n";
    echo "Matches:<pre>" .htmlspecialchars(print_r($matches, true)) ."</pre>\n\n";
}
?>
</body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接