I'd like to have a function to return the src of an image. The image should be selected by an xpath, which shall be passed to the function:
function getImgUrl($html, $xpath) {
$xpathkram = new DOMXPath(@DOMDocument::loadHTML($html));
$src = $xpathkram->evaluate('string(' . $xpath . '@src)');
return $src;
}
$xpath = '//*[@id="watch7-sidebar-modules"]/div[1]/div/div[2]/ul/li/div[2]/a/span/img';
$html = file_get_contents("https://www.youtube.com/watch?v=LNBjMRvOB5M";
echo getImgUrl($html, $xpath));
Yet for some reason it refuses to work and states a DOMXPath::evaluate(): Invalid expression
in line four.