What is the fastest way to convert html table to php array?

11,187

Solution 1

Use a DOM parser like SimpleXML to split the HTML code into nodes, and walk through the nodes to build the array.

For broken/invalid HTML, SimpleHTMLDOM is more lenient (but it's not built in).

Solution 2

String replace and explode would work if the HTML code is clean and always the same, as soon as you have new attributes it will brake. So only dependable solution would be using regular expressions or XML/HTML parser. Check http://php.net/manual/en/book.dom.php

Solution 3

An alternative to using a native DOM parser could be using YQL. This way you dont have to do the actual parsing yourself. The YQL Web Service enables applications to query, filter, and combine data from different sources across the Internet.

For instance, to grab the HTML table with the class example given at

http://www.w3schools.com/html/html_tables.asp

you can do

$yql = 'http://tinyurl.com/yql-table-grab';
$yql = json_decode(file_get_contents($yql));
print_r( $yql->query->results );

I've deliberated shortened the URL so it does not mess up the answer. $yql actually links to the YQL API, adds some options and contains the query:

select * from html 
    where xpath="//table[@class='example']" 
    and url="http://www.w3schools.com/html/html_tables.asp"

YQL can return JSON and XML. I've made it return JSON and decoded this then, which then results in a nested structure of stdClass objects and Arrays (so it's not all arrays). You have to see if that fits your needs.

You try out the interactive YQL console to see how it works.

Share:
11,187
rsk82
Author by

rsk82

Updated on June 04, 2022

Comments

  • rsk82
    rsk82 almost 2 years

    are there build in functions in latest versions of php specially designed to aid in this task ?