rsk82
rsk82

Reputation: 29397

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

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

Upvotes: 3

Views: 11364

Answers (6)

Gordon
Gordon

Reputation: 317119

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.

Upvotes: 1

pilotstarmedia.com
pilotstarmedia.com

Reputation: 577

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

Upvotes: 1

Pekka
Pekka

Reputation: 449613

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).

Upvotes: 7

phimuemue
phimuemue

Reputation: 36031

If you want to convert the html-description of a table, here's how I would do it:

You have to work out the details on your own, since I do not know if you want to handle different lines as subarrays or you want to merge all lines into one big array or something else.

Upvotes: -1

Haim Evgi
Haim Evgi

Reputation: 125564

i dont know if this is the faster , but you can check this class (using preg_replace)

http://wonshik.com/snippet/Convert-HTML-Table-into-a-PHP-Array

Upvotes: 0

rgroli
rgroli

Reputation: 1379

you could use the explode-function to turn the table cols and rows into arrays.

see: php explode

Upvotes: -2

Related Questions