如何从PHP的HTML表中提取数据? [英] How can I extract data from an HTML table in PHP?

查看:388
本文介绍了如何从PHP的HTML表中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何使用PHP解析和处理HTML?

Possible Duplicate:
How to parse and process HTML with PHP?

假设我要从此处的表格中提取某个数字/文本:

Let's say I want to extract a certain number/text from a table from here: http://www.fifa.com/associations/association=chn/ranking/gender=m/index.html

我想在 FIFA排名位置下的右表td中获得第一个数字.现在应该是 88 .经检查,为<td class="c">88</td>.

I want to get the first number on the right table td under FIFA Ranking position. That would be 88 right now. Upon inspection, it is <td class="c">88</td>.

我将如何使用PHP从所述网页中提取信息?

How would I use PHP to extract the info from said webpage?

编辑:我被告知JQuery/JavaScript就是为了这个……更适合

edit: I am told JQuery/JavaScript it is for this... better suited

推荐答案

这可能更漂亮,但它的内容类似于:

This could probably be prettier, but it'd go something like:

<?php
$page = file_get_contents("http://www.fifa.com/associations/association=chn/ranking/gender=m/index.html");
preg_match('/<td class="c">[0-9]*</td>/',$page,$matches);
foreach($matches as $match){
    echo str_replace(array( "/<td class=\"c\">", "</td>"), "", $match);
}
?>

我以前从未用PHP做过类似的事情,所以它可能行不通.

I've never done anything like this before with PHP, so it may not work.

如果您可以在页面加载后完成魔术,则可以使用JavaScript/JQuery

If you can work your magic after page load, you can use JavaScript/JQuery

<script type='text/javascript'>
var arr = [];

jQuery('table td.c').each(
    arr[] = jQuery(this).html();
);

return arr;
</script>

此外,很抱歉删除我的评论.您不确定要执行的操作,因此尽管最初jQuery会更好地满足您的需求,但我后来想到也许您想在加载HTML页面之前获取页面内容".

Also, sorry for deleting my comment. You weren't specific as to what needed to be done, so I initially though jQuery would better fit your needs, but then I thought "Maybe you want to get the page content before an HTML page is loaded".

这篇关于如何从PHP的HTML表中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆