Home > Backend Development > PHP Tutorial > Template engine regular expression debugging tips_PHP tutorial

Template engine regular expression debugging tips_PHP tutorial

WBOY
Release: 2016-07-21 15:26:45
Original
945 people have browsed it

Template engines based on regular expression replacement can easily run into the maximum backtracking/recursion limit of regular expressions.
Lazy matching is not terrible. Under normal circumstances, the template is not enough and often does not exceed the limit. Discuz's template engine is widely used. But if you don't pay attention and don't study, you will easily make mistakes and run into problems.
When preg_* returns null, you should pay attention. The judgment function is is_null.
Errors are not terrible, but it is best to output all errors completely, so that debugging is easy.
In addition to outputting the cause of the error, it also outputs the matching text and the regular expression used, so that it is easy to debug.
PHP code

Copy code The code is 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_BAD_UTF8_ERROR:
echo 'PREG_BAD_UTF8_ERROR';
break;
case PREG_BAD_UTF8_OFFSET_ERROR:
echo 'PREG_BAD_UTF8_OFFSET_ERROR'; >echo 'UNKNOW ERROR';
}
exit;
}




References

1, 2010, Laruence
"Understanding the Maximum Backtracking/Recursion Limitation of Regularity (pcre)"

2, 2011, PHP Chinese Manual preg_last_error

http://www.bkjia.com/PHPjc/323933.html

www.bkjia.com

true

TechArticleTemplate engines based on regular expression replacement can easily encounter the maximum backtracking/recursion limit of regular expressions. Lazy matching is not terrible. Under normal circumstances, the template will not be insufficient, and often it will not...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template