【发布时间】:2014-01-04 21:36:40
【问题描述】:
<?php
$html = file_get_contents('http://xpool.xram.co/index.cgi');
echo $html;
?>
我想使用 php 在远程网站的标签中获取信息。并且只有标签。
我发现这个小字符串非常适合检索整个网站源。但是,我只想得到一小部分。如何过滤掉所有其他标签,只获取我需要的一个标签?
【问题讨论】:
标签: php html html-parsing
<?php
$html = file_get_contents('http://xpool.xram.co/index.cgi');
echo $html;
?>
我想使用 php 在远程网站的标签中获取信息。并且只有标签。
我发现这个小字符串非常适合检索整个网站源。但是,我只想得到一小部分。如何过滤掉所有其他标签,只获取我需要的一个标签?
【问题讨论】:
标签: php html html-parsing
我建议使用 PHP DOM 解析器。 (http://simplehtmldom.sourceforge.net/manual.htm)
require_once ('simple_html_dom.php');
$html = file_get_contents('http://xpool.xram.co/index.cgi');
$p = $html->find('p'); // Find all p tags.
$specific_class = $html->find('.classname'); // Find elements with classname as class.
$element_id = $html->find('#element'); // Find element with the id element
阅读文档,还有很多其他可用选项。
【讨论】: