Search code examples
phpfile-get-contents

get specific data with file_get_contents()


I want to get content from a site where i want to output some specific data. The data there looks like this:

<a itemprop="email">office@xy.com</a>

From this type of data i want to output only the email adress.

This is the Code:

<?php
$homepage = file_get_contents('https://www.xy.com/');
echo $homepage;
?>

Solution

  • You should use a parser. This will be more accurate than a regex, or string functions.

    $dom = new domdocument();
    $dom->loadhtml('<a itemprop="email">office@xy.com</a>');
    $xpath = new DOMXPath($dom);
    echo $xpath->query('//a[@itemprop="email"]')[0]->nodeValue;
    

    https://3v4l.org/BU7Q4

    You can read more here.

    1. http://php.net/manual/en/class.domdocument.php
    2. https://en.wikipedia.org/wiki/XPath

    An alternative to using the xpath could be select all links then looking for the attribute.

    $dom = new domdocument();
    $dom->loadhtml('<a itemprop="email">office@xy.com</a>');
    $links = $dom->getElementsByTagName('a');
    foreach($links as $link) {
        if($link->getAttribute('itemprop') == 'email') {
            echo $link->nodeValue;
        }
    }