从元标记中获取内容值

时间:2013-06-28 16:46:19

标签: php dom xpath

考虑以下代码:

<div class="item">
  <meta content="2013-06-28" itemprop="date">
</div>
<div class="item">
  <meta content="2013-06-27" itemprop="date">
</div>
<div class="item">
  <meta content="2013-06-25" itemprop="date">
</div>
...

使用php和DOM / xPath,我如何提取日期值?

2 个答案:

答案 0 :(得分:0)

如果你只想要日期,这个xpath将全部返回:

//meta[@itemprop = "date"]/@content

答案 1 :(得分:0)

这应该有助于您入门:

<?php
$html = <<< ENDL
<div class="item">
  <meta content="2013-06-28" itemprop="date">
</div>
<div class="item">
  <meta content="2013-06-27" itemprop="date">
</div>
<div class="item">
  <meta content="2013-06-25" itemprop="date">
</div>  
ENDL;

$doc = new DomDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);

$metas = $xpath->query("*/div[@class='item']/meta");
if(count($metas)) {
  foreach($metas as $key => $element) {
      echo "[{$element->nodeName}]";
      foreach($element->attributes as $attrName => $attrValue) {
          echo "$attrName = $attrValue->nodeValue<br>";
      }
  }
} else {
  echo "No metas found!";
}
?>

将导致:

[meta]content = 2013-06-28
itemprop = date
[meta]content = 2013-06-27
itemprop = date
[meta]content = 2013-06-25
itemprop = date