Template engine regular expression debugging tips

Source: Internet
Author: User

It's been a long time since I've had a problem with PHP, and this is a bit of an issue, and it's the biggest retrospective and recursive limitation of old problems. After learning it is not difficult to modify debugging has such a problem. Mainly the following points.

A template engine that is based on regular expression substitution can easily meet the maximum backtracking/recursion restrictions for regular expressions.

Lazy matching is not scary, normally the template is not enough, often do not exceed the limit, Discuz template engine is used heavily. But instead of paying attention and not learning, it is easy to write mistakes and encounter problems.

When preg_* returns NULL, it should be noted that the judgment function is is_null.

Error is not scary, but it is best to complete the error output, so debugging is very easy.

In addition to the output error reason, but also to output matching text and the use of regular, so it is easy to debug.

PHP code

  1. <?php
  2. if (is_null($tmp)) {
  3. $error _code = Preg_last_error ();
  4. Switch($error _code) {
  5. case preg_no_error:
  6. echo ' preg_no_error ';
  7. break;
  8. case preg_internal_error:
  9. echo ' preg_internal_error ';
  10. break;
  11. case preg_backtrack_limit_error:
  12. echo ' preg_backtrack_limit_error ';
  13. break;
  14. case preg_recursion_limit_error:
  15. echo ' preg_recursion_limit_error ';
  16. break;
  17. case preg_bad_utf8_error:
  18. echo ' preg_bad_utf8_error ';
  19. break;
  20. case preg_bad_utf8_offset_error:
  21. echo ' preg_bad_utf8_offset_error ';
  22. break;
  23. default:
  24. echo ' unknow ERROR ';
  25. }
  26. exit;
  27. }

Resources

1, laruence "deep Note regular (PCRE) maximum backtracking/recursion limit"

2, PHP, Chinese manual preg_last_error

(Source: Breeze Lab )

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.