I am writing this answer because I came across the same problem. As Alan Moore pointed out, adjusting return and recursion constraints won't help solve the problem.
The described error occurs when the needle exceeds the maximum possible needle size, which is limited by the pcre base library. The described error is NOT caused by php, but the pcre base library. This is error message # 20, which is defined here:
https://github.com/php/.../pcre_compile.c#L477
php just prints errortext obtained from pcre library on error.
However, this error appears in my environment when I try to use previously captured fragments as a needle, and they are more than 32 Kbytes.
It can be easily tested using this simple script from php cli
<?php // This script demonstrates the above error and dumps an info // when the needle is too long or with 64k iterations. $expand=$needle="_^b_"; while( ! preg_match( $needle, "Stack Exchange Demo Text" ) ) { // Die after 64 kbytes of accumulated chunk needle // Adjust to 32k for a better illustration if ( strlen($expand) > 1024*64 ) die(); if ( $expand == "_^b_" ) $expand = ""; $expand .= "a"; $needle = '_^'.$needle.'_ism'; echo strlen($needle)."\n"; } ?>
To correct the error, either the corresponding needle decreased or decreased, or - if everything is necessary for capture - it is necessary to use several preg_match with the additional offset parameter.
<?php if ( preg_match( '/'.preg_quote( substr( $big_chunk, 0, 20*1024 ) // 1st 20k chars ) .'.*?'. preg_quote( substr( $big_chunk, -5 ) // last 5 ) .'/', $subject ) ) { // do stuff } // The match all needles in text attempt if ( preg_match( $needle_of_1st_32kbytes_chunk, $subj, $matches, $flags = 0, $offset = 32*1024*0 // Offset -> 0 ) && preg_match( $needle_of_2nd_32kbytes_chunk, $subj, $matches, $flags = 0, $offset = 32*1024*1 // Offset -> 32k ) // && ... as many preg matches as needed ) { // do stuff } // it would be nicer to put the texts in a foreach-loop iterating // over the existings chunks ?>
You get the idea.
Although this answer is courtesy of laaaaate, I hope that it still helps people facing this problem, without a good explanation of the reasons for the error.