OnlineSolution Forum IndexOnlineSolution Forum Index
OnlineSolution
FAQ  FAQ   Search  Search   Memberlist  Memberlist   Usergroups  Usergroups   Register  Register  
Register::  Log in Log in to check your private messages


 Advertisement

Post new topic  Reply to topic
 [ PHP ] PHP Tips - Work with long string for preg_match_all (PCRE) « View previous topic :: View next topic » 
Author Message
Paul
PostPosted: Fri Jun 11, 2010 12:13 am    Post subject: PHP Tips - Work with long string for preg_match_all (PCRE) Reply with quote

Site Admin

Joined: 10 Dec 2007
Posts: 143
Location: New Zealand

I spent awful a lot of time to work with simple preg_match_all using regex pattern.

I had a fairly big text file (around 1 MB text file) and I tried to work with some patterns that reads multiple lines and extract data out of those pattern.

The sample was something like below:

Code:
<data>
  xxxx
  xxxxxxxxxx
  ...
</data>
<data>
  xxxx
  xxxxxxxxxx
  ...
</data>
(about 1 MB length in total for 2 values)


and I was trying to extract values out of data using the following PHP code:

Code:
$data = array();

preg_match_all("%<data+>(.*?)</data+>%s",$input_text,$data,PREG_SET_ORDER);


RESULT SHOULD BE:

Code:
Array
(
    [0] => Array
        (
            [0] => <data>
  data1
  xxxxxxxxxx
  ...
</data>
            [1] =>
  data1
  xxxxxxxxxx
  ...
 
        )
 
    [1] => Array
        (
            [0] => <data>
  data2
  xxxxxxxxxx
  ...
</data>
            [1] =>
  data2
  xxxxxxxxxx
  ...
 
        )
 
)


BUT the problem was it WASN'T extracting values correctly.

So I spent hours to figure it out and tried all sorts of different combinations to make work without success.

At the end, I found an article about pcre.backtrack_limit and pcre.recursion_limit and that article told me increasing these limit will fix my problem and finally I got it working.

If you have the same problem, do with following steps:

    1. Open php.ini
    2. Find pcre.backtrack_limit and pcre.recursion_limit and change the limit.
    3. Save php.ini
    4. Restart web server if necessary


http://docs.php.net/manual/en/pcre.configuration.php
_________________
Paul KH Kim

http://www.onlinesolution.co.nz
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

 Sponsor Link

Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



OnlineSolution - Since 2007
DAJ Glass (1.0.8) template by Dustin Baccetti
EQ graphic based off of a design from www.freeclipart.nu
Powered by phpBB © 2001, 2005 phpBB Group