views:

56

answers:

3

I need to extract all HTML tags from a webpage into an array without the data inside the tags. It would look something like...

I'm using PHP

Array 
{
   html =>
             Array 
             {
                 head =>
                          Array
                          {
                              title,
                              meta name='description' content='bla bla'
                              meta name='keyword' content='bla bla'
                              ....
                          },
                 body =>
                          Array
                          {
                              div id='header' =>
                                              Array
                                              {
                                                  div class='logo',
                                                  div class='nav'
                                              },
                              div id='content' =>
                                              Array
                                              {
                                                  h1,
                                                  p class='first-para',
                                                  p,
                                                  p,
                                                  div id='ad'
                                              },
                              div id='footer' =>
                                              Array
                                              {
                                                  ul =>
                                                        Array
                                                        {
                                                            li =>
                                                                  Array
                                                                  {
                                                                     a href='link.htm'
                                                                  },
                                                            li =>
                                                                  Array
                                                                  {
                                                                     a href='link.htm'
                                                                  },
                                                            li =>
                                                                  Array
                                                                  {
                                                                     a href='link.htm'
                                                                  }
                                                        }
                                              }
                          }

             }
}
+2  A: 

Hi,

What you need is an HTML parser (an XML parser would probably not do because HTML often is invalid). Maybe: http://simplehtmldom.sourceforge.net/

Alin Purcaru
Suggested third party alternatives to [SimpleHtmlDom](http://simplehtmldom.sourceforge.net/) that actually use [DOM](http://php.net/manual/en/book.dom.php) instead of String Parsing: [phpQuery](http://code.google.com/p/phpquery/), [Zend_Dom](http://framework.zend.com/manual/en/zend.dom.html), [QueryPath](http://querypath.org/) and [FluentDom](http://www.fluentdom.org).
Gordon
@Alin DOM can load real world HTML fine when you use `loadHTML()`. That seems to be a common misconception.
Gordon
A: 

I think the simplest way is to use XPath.

//*::name()

Should give you the names of all nodes on all levels. Iam not sure wheather not hierarchy will be flattened though.

codymanix
+1  A: 

You can also use the PHP DOM extension.

YWE