0

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

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

what's wrong?

3
  • 1
    The pony he comes... Commented Aug 7, 2012 at 13:50
  • The pony he comes... DON'T USE REGULAR EXPRESSIONS TO PARSE HTML! Commented Aug 7, 2012 at 13:50
  • I do wonder if we should be able to close questions with a canned 'HTML/Regexp' reason Commented Aug 7, 2012 at 13:51

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

Comments

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;

3 Comments

And what if I want look into HTML between <tr> tags after that?
Since $matches is a DOMNodeList, each node has a property childNodes which is another DOMNodeList of its children.
@Kolink Well i may case when i have to parse an invalid HTML the DOMDocument is failed to parse.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.