在document.write PHP中获取img src值

时间:2017-07-19 10:03:43

标签: php xpath

我需要使用PHP XPath&获取以下代码中的图像src值。节点

示例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;

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

您可以在下面的php中使用DOMDocument来获取图片来源。

    $html=file_get_contents('file_path');

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