Is it possible to get the value of the background-image attribute through xpath?

2

I have the following structure:

<div class="xGh" style="background-image: url('name_file.jpg');"></div>

Where do I need to capture:

name_file.jpg

I tried to use the solution featured in this response , but not working, has a syntax error:

See test with error on Ideone

$img = $xpath->query(substring-before(substring-after(//div[@class='xGh']/@style, "background-image: url('"), "')"));    

echo $img->item($i)->nodeValue."<br/>";

I know how to do with regex , but I wanted to use xpath , is it possible?

    
asked by anonymous 01.11.2017 / 11:57

1 answer

1

Yes, it is possible. The code path is correct, you just need to fix the syntax and xpath.

<?php
$dom = new DOMDocument;
$dom->loadHTML('<div class="xGh" style="background-image: url(\'name_file.jpg\');"></div>');

$xpath = new DOMXpath($dom);
$xpatyQ = "substring-before(substring-after(//*[@class=\"xGh\"]/@style, \"background-image: url('\"), \"')\")";
$img = $xpath->query($xpatyQ);

$result = $xpath->evaluate($xpatyQ);
echo $result; // name_file.jpg

Code: link

    
01.11.2017 / 14:19