user3305327
user3305327

Reputation: 907

get value using DOMDocument

I am trying to fetch a value from the following html snippet using DOMDocument:

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

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

I need to fetch the value 465 from this code snippet. To avail this I am using the following code:

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";
                        }
         }
       }
     }
   }

But this code is not fetching the value...can anyone please help me on this.

Upvotes: 2

Views: 233

Answers (3)

Kevin
Kevin

Reputation: 41885

Inside your loop, you're pointing in the wrong object:

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

You should point to $p instead.

After that just use your current condition, if it satisfies, then loop all the child nodes:

$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

Another way is to use xpath queries:

$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

Upvotes: 0

Aleksander Wons
Aleksander Wons

Reputation: 3967

You have an invalid HTML. Where is the closing tag for meta? This is why you get the results you see.

To find what you are looking for you can use xpath:

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

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

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 98881

Use jQuery, like this:

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

Outputs:

EUR 465.0000

CODEPEN DEMO

Upvotes: 0

Related Questions