Home  >  Article  >  Backend Development  >  php regular length limit

php regular length limit

巴扎黑
巴扎黑Original
2016-11-24 09:52:421282browse

Scenario: When using regular expressions to match page content (preg_match('/(.*)(.*)(.*)/Uis',$str)), it is found that sometimes Normal matching, sometimes not, you can analyze the page structure and find that there is no change, the only change is the page size.

Reason: PHP limits the matching length of regular expressions. You can see the following with phpinfo

pcre.backtrack_limit 100000 100000
pcre.recursion_limit 100000 100000

I found that it is only 100K, which is definitely not enough if you are grabbing a web page , in fact, just make the corresponding settings on php.ini (or change the size of these two values)

pcre.backtrack_limit=-1
pcre.recursion_limit=-1

Attached note:

[Pcre]
; Perl compatible regular expression module
pcre.backtrack_limit = 100000
; The maximum number of backtracking steps for PCRE.
pcre.recursion_limit = 100000
; The maximum recursion depth of PCRE.
; If you set this value very high, you may run out of process stack space and cause PHP to crash.


Statement:
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