2017-07-19 69 views
0

我需要使用PHP XPath &节点从以下代码中获取图像的src值。在document.write中获取img src值PHP

样本HTML

<div class=\"thumb-inside\"> 
    <div class=\"thumb\"> 
     <script>document.write(thumbs.replaceThumbUrl('<a href=\"....."><img src=\".....\" /></a>'));</script> 
    </div> 
</div> 

我想是这样的:

$node = $xpath->query("div[@class='thumb-inside']/div[@class='thumb'‌​]/a/img/attribute::s‌​rc", $e); 
$th = $node->item(0)->nodeValue;
+0

你已经试过了什么? – BenM

+0

$ node = $ xpath-> query(“div [@ class ='thumb-inside']/div [@ class ='thumb']/a/img/attribute :: src”,$ e); \t \t $ th = $ node-> item(0) - > nodeValue; – romeo

+0

我这样绑定 – romeo

回答

0

我通过下面的代码来实现的。但我不知道它是否是一个正确的代码。

 $string = str_replace("document.write(thumbs.replaceThumbUrl(","",$string); 
    $string = str_replace("'));","",$string); 
    $pattern = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#'; 
    preg_match_all($pattern, $string, $matches, PREG_PATTERN_ORDER); 
    $th = $matches[0][0]; 
0

您可以使用DOMDocument在下面的php中获得图像源。

$html=file_get_contents('file_path'); 

    $doc = new \DomDocument();  
    @$doc->loadHTML($html); 
    $tags = $doc->getElementsByTagName('img'); 
    foreach ($tags as $tag) { 
     echo $tag->getAttribute('src'); 
    }