Parse html table using file_get_contents to php array

后端 未结 2 742
再見小時候
再見小時候 2020-12-05 03:28

I am trying to parse the table shown here into a multi-dimensional php array. I am using the following code but for some reason its returning an empty array. After searching

2条回答
  •  误落风尘
    2020-12-05 04:18

    Don't cripple yourself parsing HTML with regexps! Instead, let an HTML parser library worry about the structure of the markup for you.

    I suggest you to check out Simple HTML DOM (http://simplehtmldom.sourceforge.net/). It is a library specifically written to aid in solving this kind of web scraping problems in PHP. By using such a library, you can write your scraping in much less lines of codes without worrying about creating working regexps.

    In principle, with Simple HTML DOM you just write something like:

    $html = file_get_html('http://flow935.com/playlist/flowhis.HTM');
    foreach($html->find('tr') as $row) {
       // Parse table row here
    }
    

    This can be then extended to capture your data in some format, for instance to create an array of artists and corresponding titles as:

    find('tr') as $row) {
        $time = $row->find('td',0)->plaintext;
        $artist = $row->find('td',1)->plaintext;
        $title = $row->find('td',2)->plaintext;
    
        $table[$artist][$title] = true;
    }
    
    echo '
    ';
    print_r($table);
    echo '
    '; ?>

    We can see that this code can be (trivially) changed to reformat the data in any other way as well.

提交回复
热议问题