Search code examples
phpmetadatameta

Extract Meta itemprop="price" content


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!


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) { 
    
     }