我正在尝试解析这里显示的表格,将其转换为多维 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);
?>
我希望我的输出数组看起来像这样:
1 --> 11:33AM --> DEV --> 在暗中
2 --> 11:29AM --> LIL' WAYNE --> 她会
3 --> 11:26AM --> KARDINAL OFFISHALL --> NUMBA 1 (TIDE IS HIGH)