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,526
  • 11
  • 62
  • 104

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,660
  • 1
  • 12
  • 26
3

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

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
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
  • 320,036
  • 81
  • 464
  • 592