0

I am trying to extract a table from HTML, here is the HTML code for the start of the table.

<table class='price' id='comp' style='clear:both;display:none'> 

But when I use this PHP code, no matches are found

preg_match("/<table class='price' id='comp' style='clear:both;display:none'>.*?<\/[\s]*table>/s", $buffer, $matches);
print_r($matches);

As the tables are in the HTML, I guess the problem is with the preg_match statement.

user1197941
  • 179
  • 3
  • 16
  • 3
    Do not mix regex with HTML. Use some XML parser instead. – hsz Feb 09 '12 at 15:18
  • @hsz Why not? When he need only one value from whole site? Parsing (maybe) invalid HTML will probably use more resources – Vyktor Feb 09 '12 at 15:33
  • try adding `im` modifiers to the end of your regexp (so end would look like: `table>/im"` and let me know it it worked – Vyktor Feb 09 '12 at 15:34
  • [Regex is not for parsing HTML.](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454) – kapa Feb 09 '12 at 21:43

2 Answers2

0

Use PHP Simple HTML DOM Parser instead. Don't roll your own regex to extract data from webpages. If you're absolutely hell-bent on using your own regex to do this, try out My Regex Tester (not mine - that just happens to be its name) to easily debug your pattern. Also, refer to this Stack Overflow post for reasons why you shouldn't do what you're trying to do.

Community
  • 1
  • 1
WWW
  • 9,734
  • 1
  • 29
  • 33
  • I cant use the DOM parser as I am using a shared hosting. My regex for this should be simple anyway. – user1197941 Feb 09 '12 at 17:32
  • @user1197941 Why would shared hosting prevent you from using the class I linked? It's a single PHP file, and its only requirement is having PHP 5. – WWW Feb 09 '12 at 18:24
-1

Guess you need this:

<table class='price' id='comp' style='clear\:both;display\:none'>[\s\S]*?</table>

sudipto
  • 2,472
  • 1
  • 17
  • 21