0

Trying to find all HTML <table> rows with this operator, but nothing:

preg_match_all("#<tr[^>]*>.*</tr>#", $content, $matches);

what's wrong?

Dmytro Zarezenko
  • 10,308
  • 9
  • 56
  • 99

3 Answers3

4
preg_match_all ('#<tr[^>]*>(.*?)</tr>#s')

Added the "s" flag, so that it also matches newlines, a question mark to the match (lazy), and also added parenthesis (to capture the group).

DavidS
  • 1,570
  • 1
  • 11
  • 26
3

I think you'll have a lot more success with a PHP HTML parser.

Brian Agnew
  • 261,477
  • 36
  • 323
  • 432
3

Any regex will have trouble with nested tables, unless you get into complicated recursive expressions.

Try this instead:

$dom = new DOMDocument();
$dom->loadHTML($content);
$matches = $dom->getElementsByTagName("tr");
$count = $matches->length;
Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566