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

前端 未结 6 1690
深忆病人
深忆病人 2020-12-21 08:52

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

6条回答
  •  一个人的身影
    2020-12-21 09:04

    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.

提交回复
热议问题