【发布时间】:2012-01-07 09:34:43
【问题描述】:
我正在尝试将 here 显示的表解析为多维 php 数组。我正在使用以下代码,但由于某种原因它返回一个空数组。在网上搜索后,我找到了this site,这是我从中获得 parseTable() 函数的地方。通过阅读该网站上的 cmets,我发现该功能运行良好。所以我假设我从 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:33 --> 开发 --> 在黑暗中 2 --> 上午 11:29 --> 律韦恩 --> 她会的 3 --> 上午 11:26 --> 卡迪纳尔OFFISHALL --> NUMBA 1(潮位高)【问题讨论】:
-
-1 表示缺乏努力。隔离您的问题,而不是基本上发布一大段代码并要求人们找出问题所在并进行修复。
标签: php html-parsing file-get-contents