The template engine based on regular expression substitution can easily meet the limit of the maximum backtracking/recursion of regular expressions.
Lazy matching is not scary, normally the template is not enough, often does not exceed the limit, the Discuz template engine is heavily used. But instead of paying attention and not learning, it is easy to write mistakes and encounter problems.
When preg_* returns NULL, it is important to note that the judgment function is is_null.
Mistakes are not scary, but it's a good idea to have the output of the error intact, so debugging is easy.
In addition to the output error reason, but also output matching text and the use of the regular, so it is easy to debug.
PHP code
Copy the Code code as follows:
if (Is_null ($tmp)) {
$error _code = Preg_last_error ();
Switch ($error _code) {
Case PREG_NO_ERROR:
Echo ' Preg_no_error ';
Break
Case PREG_INTERNAL_ERROR:
Echo ' Preg_internal_error ';
Break
Case PREG_BACKTRACK_LIMIT_ERROR:
Echo ' Preg_backtrack_limit_error ';
Break
Case PREG_RECURSION_LIMIT_ERROR:
Echo ' Preg_recursion_limit_error ';
Break
Case PREG_BAD_UTF8_ERROR:
Echo ' Preg_bad_utf8_error ';
Break
Case PREG_BAD_UTF8_OFFSET_ERROR:
Echo ' Preg_bad_utf8_offset_error ';
Break
Default
Echo ' Unknow ERROR ';
}
Exit
}
Resources
1, laruence, "depth of the regular (PCRE) maximum backtracking/recursion limit"
2, PHP Chinese manual preg_last_error
The above describes the Perl regular Expression template engine regular expression debugging tips, including the Perl regular expression aspects of the content, I hope to be interested in PHP tutorial friends helpful.