0

I'm trying to extract to data between <table class="clubs"> .. </table> using this code:

preg_match('#\<table class\="clubs"\>(.*?)\</table\>#', $raw_data, $new_data);

My HTML markup:

but i get empty array! what's wrong? I believe everything is escaped correctly

Dor Moshkovitz
  • 371
  • 1
  • 3
  • 9
  • 2
    Don't use a regex. Use a DOM parser instead. – Amal Murali Feb 23 '14 at 17:37
  • I'd guess you need the whatever that option was to match across multiple lines. (Or, better yet, as @AmalMurali says, use a DOM parser) – towr Feb 23 '14 at 17:38
  • The is one interesting post I read few hour back about `html` and `regex` http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – ElectricRouge Feb 23 '14 at 17:40
  • 2
    @ElectricRouge: Please. That doesn't help the OP. I know it's cool and all, but that's not helpful. Stop linking to that. – Amal Murali Feb 23 '14 at 17:45

1 Answers1

3

The main problem is that . doesn't match newlines. You can fix it by using the s modifier to make . match newlines as well.

The second problem is that you're using //// while there should be only one /. You also have a lot of unnecessary escapes:

preg_match('#<table class="clubs">(.*?)</table>#s', $raw_data, $new_data);

Should work. regex101 demo

That said, you would be better off with a parser dedicated to parse HTML.

Jerry
  • 70,495
  • 13
  • 100
  • 144