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

使用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

图片转代码服务由CSDN问答提供 功能建议

我有以下结构:

 &lt; div class  =“xGh”style =“background-image:url('name_file.jpg');”&gt;&lt; / div&gt; 
   
 
 

我需要输出:

name_file.jpg

我尝试使用回答,但对我不起作用:

  $ img = $  xpath-&gt; query(substring-before(substring-after(// div [@ class ='xGh'] / @ style,“background-image:url('”),“')”));  
 
echo $ img-&gt; item($ i) - &gt; nodeValue。“&lt; br /&gt;”; 
   
 
 

查看错误:< / p>

测试 Ideone ,错误

我在sintaxe中有一些错误,我无法看到它是如何正确的...

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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题