dtlab08822 2017-11-01 05:47
浏览 1285
已采纳

使用xpath从background-image样式属性中提取值

Ii have the follow structure:

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

I need that output:

name_file.jpg

I try use that answer, but dont work fo me :

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

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

Look the error:

Test Ideone with error

I have some error in that sintaxe i cant see how is correct...

sry my english

  • 写回答

1条回答 默认 最新

  • dongxun1142 2017-11-01 13:40
    关注

    1) You lost quotes wrapping xpath - it's string.

    2) with dom xpath, query returns set of nodes while to receive string result it's better to use evaluate

    $img = $xpath->evaluate('substring-before(substring-after(//div[@class=\'xGh\']/@style, "background-image: url(\'"), "\')")');    
    
    echo $img; // it contains name_file.jpg
    

    demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问