Simon
Simon

Reputation: 5698

Analyzing HTML Page

I've got a question that concerns the analyzing of HTML pages. For example there is an page, www.example.com/page.html that contains information in tables that I need, and www.example.com/page2.html has some other information, but in text format. Currently, I'm using an regex (preg_match_all) in which I had to insert a pattern, hand made. Is there a faster/better way to do this. So the full question would be: is there a fast/good way to extract information from an HTML page that doesn't need me to use and edit parts of the source via a regex?

(Other information: I'm using PHP i.c.w. cURL to get the page's content, then I use preg_match_all to extract the data)

Upvotes: 1

Views: 231

Answers (3)

Hubro
Hubro

Reputation: 59333

Yes! You can load the content of the webpage into a PHP DOMDocument and fetch the data using html classes and IDs just as you would using Javascript.

Here is the documentation http://www.php.net/manual/en/class.domdocument.php

You should start off by using

DOMDocument::loadHTML($html);

Then follow the documentation and it's examples

Upvotes: 4

Wes
Wes

Reputation: 6585

Use any of the parsers suggested in this post. You should never use regular expressions to parse html.

Upvotes: 2

Finbarr
Finbarr

Reputation: 32126

You can use dom.

Upvotes: 1

Related Questions