Template engine regular expression debugging tips _ PHP Tutorial

Source: Internet
Author: User
Tags preg
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.