6

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.

user1651105
  • 1,727
  • 4
  • 25
  • 45

2 Answers2

11

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.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • Hey, thanks for reply, but i think i am doing something wrong. I use wampServer, i have gone to php.ini file and changed the pcre setting to 1 million, restarted all services, but i still get the same result :( Just plain word "Found " and nothing more. – user1651105 Jun 11 '10 at 09:02
  • @aleluja In your script (at the top) add `error_reporting(E_ALL ^ E_STRICT);` and try it again. Also, you may want to post your actual script to see if it can be reproduced. – cletus Jun 11 '10 at 09:08
  • I did something very silly. It does work. As you can see, in my regexp i used "[" and "]" characters, and i totally forgot they are special, i forgot to escape them, because these 2 characters were a part of a text i search for. I escaped them and now i get the result i was expecting for. Thanks for the help! – user1651105 Jun 11 '10 at 10:26
  • Don't follow @cletus answer, that is dangerous. It is about the stack size managed by the OS itself. See https://stackoverflow.com/a/7627962/1077650 – 4wk_ Nov 22 '18 at 10:20
1

No, don't up the pcre limit. Don't do things without understand them. This is a common bug with php pcre

Read this awesome answer by @ridgerunner : https://stackoverflow.com/a/7627962/1077650

this class of regex will repeatably (and silently) crash Apache/PHP with an unhandled segmentation fault due to a stack overflow!

PHP Bug 1: PHP sets: pcre.recursion_limit too large.

PHP Bug 2: preg_match() does not return FALSE on error.

Community
  • 1
  • 1
4wk_
  • 2,458
  • 3
  • 34
  • 46