使用 DOMDocument 获取值

get value using 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";
                        }
         }
       }
     }
   }

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

使用jQuery,像这样:

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

输出:

EUR 465.0000

CODEPEN DEMO

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

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

您有一个无效 HTML。 meta 的结束标记在哪里?这就是您获得所见结果的原因。

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

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

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