0

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.

kevin
  • 115
  • 8

2 Answers2

0

You have some syntax errors in code: missed closing parenthesis after file_get_contents("https://www.youtube.com/watch?v=LNBjMRvOB5M"; and two brackets instead of one here echo getImgUrl($html, $xpath));.

Slava
  • 878
  • 5
  • 8
  • I edited the code in an effort to make my problem more clear. These syntax errors slipped in in the process. – kevin Feb 01 '17 at 15:51
0

First add attribure 'src' to $xpath query as '/@src':

$xpath = '//*[@id="watch7-sidebar-modules"]/div[1]/div/div[2]/ul/li/div[2]/a/span/img/@src';

Your mistake here:

$src = $xpathkram->evaluate('string(' . $xpath . '@src)');

You shall write:

$src = $xpathkram->evaluate( $xpath );

The result is that you receive will be DOMNodeList object. To get text from it:

$list = getImgUrl($html, $xpath);

foreach ($list as $item){
    var_dump($item->textContent);
}
Ans
  • 527
  • 5
  • 9
  • This fixed it, yet it still does not work when the xpath starts with /html/.. example: [link](https://blog.google/topics/google-europe/zaha-hadid-serpentine-galleries-virtual-reality/), `/html/body/main/article/section[3]/div/nav/a[2]/figure/picture/img/@src` – kevin Feb 01 '17 at 16:00
  • try to start expression with ".//" – Ans Feb 01 '17 at 16:50
  • Unfortunately this did not help. – kevin Feb 01 '17 at 17:12
  • Stangely it only seems to work if I use some sort of class or id as a selector (with brackets) – kevin Feb 01 '17 at 17:31
  • In yours case is no matter from what symbol you start your expression - with dot or without dot. To get result, use this expression ".//main/article/section[3]/div/nav/a[2]/figure/picture/source/source/img/@src" – Ans Feb 01 '17 at 19:05