使用file_get_contents将html表解析为php数组

Far*_*mad 13 php file-get-contents html-parsing

我试图将这里显示的表解析成一个多维的PHP数组.我使用以下代码但由于某种原因它返回一个空数组.在网上搜索后,我发现这个网站是我从中获得parseTable()函数的地方.通过阅读该网站上的评论,我发现该功能完美无缺.所以我假设我从file_get_contents()获取HTML代码的方式有问题.对我做错了什么的想法?

<?php

$data = file_get_contents('http://flow935.com/playlist/flowhis.HTM');

function parseTable($html)
{
  // Find the table
  preg_match("/<table.*?>.*?<\/[\s]*table>/s", $html, $table_html);

  // Get title for each row
  preg_match_all("/<th.*?>(.*?)<\/[\s]*th>/", $table_html[0], $matches);
  $row_headers = $matches[1];

  // Iterate each row
  preg_match_all("/<tr.*?>(.*?)<\/[\s]*tr>/s", $table_html[0], $matches);

  $table = array();

  foreach($matches[1] as $row_html)
  {
    preg_match_all("/<td.*?>(.*?)<\/[\s]*td>/", $row_html, $td_matches);
    $row = array();
    for($i=0; $i<count($td_matches[1]); $i++)
    {
      $td = strip_tags(html_entity_decode($td_matches[1][$i]));
      $row[$row_headers[$i]] = $td;
    }

    if(count($row) > 0)
      $table[] = $row;
  }
  return $table;
}

$output = parseTable($data);

print_r($output);

?>
Run Code Online (Sandbox Code Playgroud)

我希望我的输出数组看起来像这样:

1
--> 11:33AM
--> DEV
--> IN THE DARK

2
--> 11:29AM
--> LIL' WAYNE
--> SHE WILL

3
--> 11:26AM
--> KARDINAL OFFISHALL
--> NUMBA 1 (TIDE IS HIGH)

jsa*_*nen 49

不要削弱自己使用regexp解析HTML!相反,让HTML解析器库为您担心标记的结构.

我建议你查看Simple HTML DOM(http://simplehtmldom.sourceforge.net/).它是专门为帮助解决PHP中的这种Web抓取问题而编写的库.通过使用这样的库,您可以用更少的代码行编写抓取工具,而无需担心创建工作正则表达式.

原则上,使用Simple HTML DOM,您只需编写如下内容:

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

然后可以将其扩展为以某种格式捕获数据,例如创建艺术家数组和相应的标题:

<?php
require('simple_html_dom.php');

$table = array();

$html = file_get_html('http://flow935.com/playlist/flowhis.HTM');
foreach($html->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 '<pre>';
print_r($table);
echo '</pre>';

?>
Run Code Online (Sandbox Code Playgroud)

我们可以看到,这个代码可以(通常)更改为以任何其他方式重新格式化数据.


Joh*_*ger 18

我尝试了simple_html_dom,但是在较大的文件和重复调用函数时,我在php 5.3(GAH)上获得了zend_mm_heap_corrupted.我也尝试了preg_match_all(但这已经失败了一个更大的文件(5000)行的HTML,这只是我的HTML表的大约400行.

我正在使用它,它的工作速度快,不会出现错误.

$dom = new DOMDocument();  

//load the html  
$html = $dom->loadHTMLFile("htmltable.html");  

  //discard white space   
$dom->preserveWhiteSpace = false;   

  //the table by its tag name  
$tables = $dom->getElementsByTagName('table');   


    //get all rows from the table  
$rows = $tables->item(0)->getElementsByTagName('tr');   
  // get each column by tag name  
$cols = $rows->item(0)->getElementsByTagName('th');   
$row_headers = NULL;
foreach ($cols as $node) {
    //print $node->nodeValue."\n";   
    $row_headers[] = $node->nodeValue;
}   

$table = array();
  //get all rows from the table  
$rows = $tables->item(0)->getElementsByTagName('tr');   
foreach ($rows as $row)   
{   
   // get each column by tag name  
    $cols = $row->getElementsByTagName('td');   
    $row = array();
    $i=0;
    foreach ($cols as $node) {
        # code...
        //print $node->nodeValue."\n";   
        if($row_headers==NULL)
            $row[] = $node->nodeValue;
        else
            $row[$row_headers[$i]] = $node->nodeValue;
        $i++;
    }   
    $table[] = $row;
}   

var_dump($table);
Run Code Online (Sandbox Code Playgroud)

这段代码对我很有用.原始代码的示例在这里.

http://techgossipz.blogspot.co.nz/2010/02/how-to-parse-html-using-dom-with-php.html