Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
How to parse and process HTML with PHP?
i need help. I have HTML, and i need regular expression which give me table. only one table. because after this table goes another tables. example html:
<table class="results" cellspacing="1" cellpadding="0" border="0" width="100%" align="left">
<tr><td>text</td></tr>
</table>
<!-style>
tr.bg_selected{}
tr.bg_selected td, tr.bg_checked td { background-color:#ffe9bc !important;}
</style>**AND ANOTHER TABLE**
its my regular. there i get all tables after this table.
$regular = "/<table class=\"results\" cellspacing=\"(\d+)\" cellpadding=\"(\d+)\" border=\"(\d+)\" (.*)>(.*)<\/table>
(.*)<\/style>/s";
preg_match_all($regular,$str, $matches2, PREG_PATTERN_ORDER);
Some people have pointed out in the comments that you "can't parse HTML in regex". This isn't entirely accurate; it can be done.
However, it's difficult and error prone, and at the end of it you get a bit of a messy structure to work with.
I would therefore strongly recommend using PHP's built in HTML parser instead. It's very simple to use:
$doc = new DOMDocument();
$doc->loadHTML($htmlCode);
You can then work with the resulting object to extract the data you need.
$tables = $dom->getElementsByTagName('table');
foreach ($tables as $table){
$cells = $table->getElementsByTagName('td');
foreach ($cells as $cell){
echo $cell->nodeValue;
}
}
See the PHP manual for more info: http://php.net/manual/en/book.dom.php