Kam K
Kam K

Reputation: 335

How do I use DOMDocument to retrieve the img path?

I have following HTML page:

 <div class="showtime-bg">
                <div style="float:left;">
                    <a href="javascript:getinfo(295, 1)"><img src="images/main/movies/movie.jpg" height="180" alt=""></a>

    </div>


$dom = new DOMDocument();  
@$dom->loadHTML($page);
$xpath_imgPath = new DOMXPath($dom);

$rightDivText_movie_path=$xpath_imgPath->query("//div[@class='showtime-bg']/div[0]/a/img[@src]");   

How can I get the path using DOMDocument?

I am not able to retrieve the path.

Upvotes: 1

Views: 70

Answers (1)

Amal Murali
Amal Murali

Reputation: 76656

The problem with your code is that you're using 0 as the base index. XPath starts the count from 1. You need to use div[1] instead of div[0]. Or just div. Here's how you can get the image src:

$xPathQuery = "//div[@class='showtime-bg']/div/a/img";
$rightDivText_movie_path=$xpath_imgPath->query($xPathQuery);   
echo $rightDivText_movie_path->item(0)->getAttribute('src');

Demo

Upvotes: 1

Related Questions