Tips for template engine regular expression debugging. The template engine based on regular expression replacement is prone to the limit of maximum backtracking and recursion of regular expressions. Normally, the template is not enough. The template engine, which is often not replaced by a regular expression, is prone to the limit of maximum backtracking/recursion of the regular expression.
The inertia matching is not terrible. under normal circumstances, the template is not enough, and the limit is often not exceeded. the discuz template engine is used in large quantities. However, if you do not pay attention to or study, you may encounter errors and problems.
When preg _ * returns null, you must note that the function is is_null.
Errors are not terrible, but it is best to output all the errors so that debugging is easy.
In addition to the error cause, the matched text and the regular expression are also output, which makes debugging easy.
PHP 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_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;
}
References
1. 2010, Laruence "deep understanding of the maximum backtracking/recursion restrictions of regular expressions (pcre)"
2. 2011, PHP Chinese manual preg_last_error
Bytes. The inertia matching is not terrible. under normal circumstances, the template will not be insufficient and will not often be used...