Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum length of string preg_match_all can match and acquire?

Tags:

regex

php

I'm trying to parse some web pages with preg_match_all() and some of them are quite large as several MBs in size. And one of the regular expressions matches some text strings that are so large that they don't seem to be able to match and acquire them. It simply returns an empty string.

One of the strings is 1.32MB or 1,393,557 bytes when I manually selected it and saved it as a .txt file.

When the string is much shorter as just tens of thousands of bytes, that regular expression successfully matches and acquires it.

So my question is, as it occurs to me there's a limit / maximum length of string preg_match_all() can match, what is it and how can I set it larger?

like image 700
datasn.io Avatar asked Nov 02 '25 23:11

datasn.io


1 Answers

Set the ini_set('pcre.backtrack_limit', '1048576'); to whatever you want in your script or on your php.ini file for global use. (example is 1mb)

Credit to: http://www.karlrixon.co.uk/writing/php-regular-expression-fails-silently-on-long-strings/

like image 122
probablyup Avatar answered Nov 04 '25 14:11

probablyup