Just trying to preg_match the second match.
<?php
$url = "http://domain.com";
preg_match('~<table([^>]*)(class\\s*=\\s*["\']ladder-table["\'])([^>]*)>(.*?)</table>~i', file_get_contents($url), $match);
print $match[0];
?>
Here is the table I’m trying to find:
<table class="ladder-table">Content</table>
<table class="ladder-table">Content</table> <-- [This one]
<table class="ladder-table">Content</table>
The last two tables are hidden by a java script code. Does it influence on the pattern?
If you want to continue to use regular expressions, use preg_match_all:
This may be enough for your requirements. However, it’s difficult to make your code robust enough to deal with changes to the HTML; for instance, the above wouldn’t match if
Contenthas any new lines, because you’re checking for.*?without thePCRE_DOTALLmodifier.The correct way to handle this would be using a proper HTML parser such as DOM or others.