Convert a (nested)HTML unordered list of links to PHP array of links

Posted by Klark on Stack Overflow See other posts from Stack Overflow or by Klark
Published on 2010-04-11T15:14:27Z Indexed on 2010/04/11 15:23 UTC
Read the original article Hit count: 552

Filed under:
|
|

Hi,

I have a regular, nested HTML unordered list of links, and I'd like to scrape it with PHP and convert it to an array.

The original list looks something like this:

<ul>
<li><a href="http://someurl.com">First item</a>
    <ul>
    <li><a href="http://someotherurl.com/">Child of First Item</a></li>
    <li><a href="http://someotherurl.com/">Second Child of First Item</a></li>
    </ul>
</li>
<li><a href="http://bogusurl.com">Second item</a></li>
<li><a href="http://bogusurl.com">Third item</a></li>
<li><a href="http://bogusurl.com">Fourth item</a></li>
</ul>

Any of the items can have children.

(The actual screen scraping is not a problem, I can do that.)

I'd like to turn this into a PHP array, of just the links, while keeping the hierarchical nature of the list. Any ideas?

I've looked at using htmlsimpledom and phpQuery, which both use jQuery like syntax. But, I can't seem to get the syntax right.

Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about html