1

If I have the following html:

<div id="thisID">100</div>

I can get the value, 100, like this:

$dom = new DOMDocument();
$dom->loadHTMLFile($url);
$data = $dom->getElementById("thisID");
$result = $data->nodeValue;`

But what about this html?

<span class="foo" id="bar" itemprop="price">100</span>

Is there a way I can get an element content by a tag variable and value, in this case itemprop="price"?

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • Use DomXpath - http://stackoverflow.com/a/6827943/1164491 – Cheery Nov 17 '14 at 20:41
  • Related: http://stackoverflow.com/questions/11064741/getelementbyattribute-php-dom http://stackoverflow.com/questions/11064741/getelementbyattribute-php-dom – scrowler Nov 17 '14 at 20:41

1 Answers1

0

a) Use DOMXPath:

<?php

$doc = new DOMDocument();
$doc->loadHTML('<span class="foo" id="bar" itemprop="price">100</span>');

$xpath = new DOMXPath($doc);
$result = $xpath->evaluate('number(//*[@itemprop="price"])');

b) Use a real microdata parser.

Alf Eaton
  • 5,226
  • 4
  • 45
  • 50