使用DOMDocument获取值

时间:2015-04-21 12:45:47

标签: php html domdocument

我正在尝试使用DOMDocument从以下html代码段中获取值:

<h3>
    <meta itemprop="priceCurrency" content="EUR">€ 

    <meta itemprop="price" content="465.0000">465
</h3>

我需要从此代码段中获取值465。为了解决这个问题,我使用以下代码:

foreach($dom->getElementsByTagName('h3') as $h) {
  foreach($h->getElementsByTagName('meta') as $p) {

    if($h->getAttribute('itemprop') == 'price') {
        foreach($h->childNodes as $child) {
            $name = $child->nodeValue;
                        echo $name;
                        $name = preg_replace('/[^0-9\,]/', '', $name);
                      //  $name = number_format($name, 2, ',', ' ');
                        if (strpos($name,',') == false) 
                        {
                         $name = $name .",00";
                        }
         }
       }
     }
   }

但是这段代码没有取得价值......任何人都可以帮我解决这个问题。

3 个答案:

答案 0 :(得分:1)

您的HTML无效。 meta的结束标记在哪里?这就是你得到你看到的结果的原因。

要查找您要查找的内容,可以使用xpath:

$doc = new \DOMDocument();
$doc->loadXML($yourHTML);

$xpath = new DOMXpath($doc);
$elements = $xpath->query("//meta[@itemprop='price']");
echo $elements->item(0)->textContent;

答案 1 :(得分:0)

使用jQuery,如下所示:

  var priceCurrency = $('meta[itemprop="priceCurrency"]').attr("content");
  var price = $('meta[itemprop="price"]').attr("content");
  alert(priceCurrency + " " + price);

输出:

EUR 465.0000

CODEPEN DEMO

答案 2 :(得分:0)

在你的循环中,你指向错误的对象:

foreach($h->childNodes as $child) {
//      ^ its not supposed to be `$h`

您应该指向$p

之后只需使用当前条件,如果满足,则循环所有子节点:

$price = '';
foreach($dom->getElementsByTagName('h3') as $h) {

    foreach($h->getElementsByTagName('meta') as $p) {

        if($p->getAttribute('itemprop') === 'price') {
            foreach($h->childNodes as $c) {
                if($c->nodeType == XML_TEXT_NODE) {
                    $price .= trim($c->textContent);
                }
            }
            if(strpos($price, ',') === false) {
                $price .= ',00';
            }
        }



    }
}

Sample Output

另一种方法是使用xpath查询:

$xpath = new DOMXpath($dom);

$meta = $xpath->query('//h3/meta[@itemprop="price"]');
if($meta->length > 0) { // found
    $price = trim($xpath->evaluate('string(./following-sibling::text()[1])', $meta->item(0)));
    if(strpos($price, ',') === false) { $price .= ',00'; }
    $currency = $xpath->evaluate('string(./preceding-sibling::meta[@itemprop="priceCurrency"]/following-sibling::text()[1])', $meta->item(0));
    $price = "{$currency} {$price}";
    echo $price;
}

Out