• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

PHP preg_replace BLANK PAGE (Too many results)...

Hi, I use preg_replace, but when there are to many results, it works really randomly. Sometimes it displays data, sometimes it doesn't.

$tableBody     = preg_replace("#^.*<tbody>(.*?)</tbody>.*$#is", "$1", $htm);

When there are 2000 results - $tableBody = nothing even with:

      ini_set('pcre.backtrack_limit', 10000000);
      ini_set('pcre.backtrack_limit', 10000000);

Sometimes it works with the same amount (2000 matches, each match consists of 9 lines), but sometimes it doesn't... how to fix this?
0
VAL1N
Asked:
VAL1N
  • 2
1 Solution
 
Ray PaseurCommented:
Please post the link to the original test data where you've encountered an issue.  There are other ways to scrape this data out, that do not require putting a lot of stress on the REGEX engine.
0
 
VAL1NAuthor Commented:
http://www.atsoftiqx.com/data.html - DATA.

$tableBody     = preg_replace("#^.*<tbody>(.*?)</tbody>.*$#is", "$1", $htm);
preg_match_all("#<tr>(?:(?!</tr>).)*?<td>(.*?)</td>(?:(?!</tr>).)*?<td>(.*?)</td>(?:(?!</tr>).)*?<td>(.*?)</td>(?:(?!</tr>).)*?<td>(.*?)</td>(?:(?!</tr>).)*?<td>(.*?)</td>(?:(?!</tr>).)*?<td>(.*?)</td>#si", $tableBody, $matches);

                              foreach ($matches[1] as $num => $value) {
                                                      $tmp_date             = $matches[3][$num];
                                                      $game_master_original = $matches[2][$num];
}
0
 
Terry WoodsIT GuruCommented:
The pattern you're using has a greedy .* that will mean you only get a single match (and replace). However, your test data only has the only pair of tbody tags, so that is likely what you want.

Using this pattern might be more efficient & reliable, and should achieve the same result:

$tableBody     = preg_replace("#^.*?<tbody>|</tbody>.*$#is", "", $htm);

Open in new window


However, I'm not particularly experienced with dealing with large quantities of data so there may be an even better way.
0
 
Ray PaseurCommented:
Nevermind...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now