使用file_get_contents将html表解析为php数组 [英] Parse html table using file_get_contents to php array
问题描述
我正在尝试在此处中显示的表解析为多维php数组.我正在使用以下代码,但由于某种原因,它返回一个空数组.在网上搜索后,我发现此网站,这是我从中获得parseTable()函数的地方.通过阅读该网站上的评论,我发现该功能运行良好.因此,我假设我从file_get_contents()获取HTML代码的方式存在问题.对我在做什么错有任何想法吗?
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 around on the web, I found this site which is where I got the parseTable() function from. From reading the comments on that website, I see that the function works perfectly. So I'm assuming there is something wrong with the way I'm getting the HTML code from file_get_contents(). Any thoughts on what I'm doing wrong?
<?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);
?>
我希望我的输出数组看起来像这样:
I want my output array to look something like this:
1
--> 11:33AM
--> DEV
--> IN THE DARK
2
--> 11:29AM
--> LIL' WAYNE
--> SHE WILL
3
--> 11:26AM
--> KARDINAL OFFISHALL
--> NUMBA 1 (TIDE IS HIGH)
推荐答案
不要让自己使用正则表达式来解析HTML!而是让HTML解析器库为您担心标记的结构
Don't cripple yourself parsing HTML with regexps! Instead, let an HTML parser library worry about the structure of the markup for you.
我建议您检查一下简单HTML DOM( http://simplehtmldom.sourceforge.net/).它是专门为帮助解决PHP中此类Web抓取问题而编写的库.通过使用这样的库,您可以用更少的代码行编写抓取代码,而不必担心创建有效的正则表达式.
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.
原则上,使用简单HTML DOM,您只需编写如下内容:
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:
<?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>';
?>
我们可以看到也可以(简单地)更改此代码以用其他任何方式重新格式化数据.
We can see that this code can be (trivially) changed to reformat the data in any other way as well.
这篇关于使用file_get_contents将html表解析为php数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!