2017-07-19 14 views
0

PHP XPath &ノードを使用して、次のコードからイメージのsrc値を取得する必要があります。ドキュメント内に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 [@クラス= 'サム']/a/img /属性:: 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を次のように使用して、画像ソースを取得できます。

$html=file_get_contents('file_path'); 

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