0

我正在使用从另一个网站获取数据file_get_contents(),但我需要提取一个<table>带有 class 的元素inputpanelfields,例如:

...
<table class="inputpanelfields">
<!-- this is what I need -->
</table>
...

我怎样才能做到这一点?

4

3 回答 3

2
$body = file_get_contents('http://example.org/path/to/page');
$d = new DOMDocument;
libxml_use_internal_errors(true);
$d->loadHTML($body);
libxml_clear_errors();

$x = new DOMXPath($d);
if (($table = $x->query('//table[contains(@class, "inputpanelfields")]'))) {
    echo $d->saveHTML($table->item(0));
}

演示

于 2013-02-15T05:18:15.260 回答
0

您正在尝试使用 PHP 执行“Javascript 工作”。如果您的内容是 XHTML,最好的方法应该是将内容加载到 DOMDocument 中,然后使用 XPath 搜索您的特定节点,最后检索元素的输出。

在这里检查:http: //php.net/manual/fr/domdocument.loadhtml.php

注意:不确定是否可行,但您可以尝试。

于 2013-02-15T05:17:59.257 回答
0

您将不得不构建一个函数来查找并提取它。

您可以使用 PHP stristr 检查表是否存在。函数 substr_count 会告诉你它是否存在不止一次。

假设它在页面上一次,让 $pagepart = your file_get_contents 和 $term = "inputpanelfields"

那么这应该从页面中抓取表格

$end = mb_stristr($pagepart,$term,false); // Case insensitive; finds the first occurrence
$beg = mb_stristr($pagepart,$term,true); // Case insensitive; finds the first occurrence
$end = mb_stristr($end,'</table>',true); // Case insensitive
$beg = mb_strrichr($beg,'<table',false); // Case insensitive
$beg = str_replace($term,'',$beg);

希望这可以帮助。

史蒂夫

于 2013-02-15T05:19:23.937 回答