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;
?>
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;
You can read more here.
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;
}
}