【问题标题】:How To Filter Out Content Grabbed By file_get_contents?如何过滤掉由 file_get_contents 抓取的内容?
【发布时间】:2014-05-18 12:54:38
【问题描述】:

我在变量 $html 中使用这一行获取页面的源代码:

$html = file_get_contents('http://www.google.com');

当我这样做时<textarea><?php echo htmlentities($html); ?></textarea>

效果很好。

现在,假设我想取出页面中的每个<h1> 标记,其内容在变量 $h1 中,我该如何从 $html 变量中执行此操作?

【问题讨论】:

标签: php html file file-get-contents


【解决方案1】:

您可以使用Simple Html Dom Parser

下载所需文件并尝试以下代码:

<?php

require_once ('simple_html_dom.php');
$html = file_get_contents('http://www.google.com');
$domHtml = str_get_html($html);
foreach ($domHtml->find('h1') as $element) {
    echo '<h1>' . $element->innertext . '</h1>';
}

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 2011-12-03
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多