Question

A given URL has code :-

  <meta itemprop="price" content="12.00" />

I want to extract 12 to a new variable, I have no idea from where to begin because here we cannot use Tags PHP function which is used to extract normal meta tags!

Was it helpful?

Solution

In order to get all meta tags you should make use of XPath to select all nodes

$xmlsource = 'http://www.example.com/';
$d = new DOMDocument();
$d->loadHTML($xmlsource);
$xpath = new DOMXPath($d);
//find all elements with itemprop attribute
$nodes = $xpath->query('//*[@itemprop]');  
foreach ($nodes as $node) { 

 }

OTHER TIPS

You can also use DOMDocument::getElementsByTagName:

$string  = file_get_contents('http://www.example.com/');
$dom = new DOMDocument();
$dom->loadHTML($string);
$dom->preserveWhiteSpace = false;
//get all meta tags
$el = $dom->getElementsByTagName('meta');

echo'<pre>';
print_r($el);
echo'</pre>';

foreach($el as $val){
    //get value of each content
    echo $val -> getAttribute('content').'<br>';
}

The XPath filter would be //meta[@itemprop='price']/@content

if you were in Google Sheets you could use the importXML formula as follows....

=importxml("http://www.example.com/product-specific-url-here", "//meta[@itemprop='price']/@content")

Is that what you were looking for?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top