Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match and long strings

This is the preg_match i am trying to use to find specific text in text file.

if (preg_match($regexp,$textFile,$result) > 0) {
    echo "Found ".$result[0];
} else {
    echo "Not found";
}

However, the result is always Found and nothing more. The result array is empty. Now i read that preg_match can't work with long strings.

My text file is about 300KB so thats 300000 characters i guess.

I am 100% sure that the searched string is in the text file, and the fact that preg_match function returns value above 0 means it found it, but it didn't place it into the result array somehow.

So my question would be, how do i make it work?

regexp would be /[specific text]\{(\d*)\}/ so, of course i want to be able to get the number in the parentheses.

like image 684
user1651105 Avatar asked Feb 26 '23 21:02

user1651105


1 Answers

You'll be glad I found this question. As of PHP 5.2, they introduced a limit on the size of text that the PCRE functions can be used on, which defaults to 100k. That's not so bad. The bad part is that it silently fails if greater than that.

The solution? Up the limit. The initialization parameter is pcre.backtrack_limit.

like image 55
cletus Avatar answered Mar 13 '23 04:03

cletus