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

Asked

Viewed 67 times

2

I have the following structure:

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

Where I need to capture:

name_file.jpg

I tried to use the solution presented in this reply, but it is not working, it presents a syntax error:

See the test with the error in 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 wanted to use xpath, it is possible?

1 answer

1


Yes, it is possible. The code path is correct, it is only necessary 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: http://sandbox.onlinephpfunctions.com/code/b8b25d86eb97b9d59fe6155618acbc78565d2470

  • See if you can help me man https://answall.com/questions/251666/howto iterate to get all the results:)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.