【问题标题】:get some content in file_get_content function [closed]在 file_get_content 函数中获取一些内容 [关闭]
【发布时间】:2011-11-18 15:06:16
【问题描述】:

我使用file_get_content 来获取网站的内容。这将获取所有内容,但我只需要我获得的内容中的 <div class="nomal"> 标记中的内容。它有一些<div class="nomal"> 标签,我需要获取所有<div class="nomal"> 标签。我的功能:

function get_need_content($str, $strstart, $strend){
    $new_startstr = strpos($str, $strstart);
    $new_strend = strpos($str, $strend);
    echo $final_str=substr($str, $new_startstr, $new_strend );
}

这个函数只得到一个<div class="nomal">标签。我需要得到所有。

【问题讨论】:

  • 您可以使用 DOM 解析器,例如 DOMDocument 来获取您需要的数据。

标签: php html


【解决方案1】:

您可能最好使用带有 XPath 查询的 SimpleXML 来提取您想要的标签的所有实例:

$str = '
<html>
  <head>
  </head>
  <body>
    <p>one</p>
    <p>two</p>
    <p>three</p>
  </body>
</html>
';

$xml = new SimpleXMLElement($str);
foreach ($xml->xpath('//p') as $item) {
    print_r($item);
}

【讨论】:

    【解决方案2】:

    如果您在 file_get_content 的返回中寻找特定标签,您可以运行 preg_match 来获取您正在寻找的值,或者因为您说它是一个网站,您可以将该网站加载为 SimpleXMLElement 或DOMDocument 来获取特定标签的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-10
      • 2015-09-30
      • 2012-12-04
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多