Home > Article > Backend Development > perl regular expression template engine regular expression debugging tips
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.
Be careful when preg_* returns null, the judgment function is is_null.
It is not terrible to make mistakes, 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, making it 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;
echo 'PREG_BACKTRACK_LIMIT_ERROR';
case PREG_RECURSION_LIMIT_ERROR:
echo 'PREG_RECURSION_LIMIT_ERROR';
break;
case
echo 'PREG_BAD_UTF8_OFFSET_ERROR';
break;
case PREG_BAD_UTF8_OFFSET_ERROR:
echo 'PREG_BAD_UTF8_OFFSET_ERROR';
break;
default:
echo 'UNKNOW RROR';
}
exit;
}
"Understanding the Maximum Backtracking/Recursion Limitation of Regularity (pcre)"
2, 2011, PHP Chinese Manual preg_last_error
The above introduces the perl regular expression and template engine regular expression debugging tips, including the content of perl regular expression. I hope it will be helpful to friends who are interested in PHP tutorials.