somejkuser
somejkuser

Reputation: 9040

xpath getting first node query

I'm scanning a webpage for images and I only want the first image it finds.

Here's my xpath:

$image = '//div[@id="content"]//div[@class="entry"]//(img)[1]//@src';

Im getting a blank array though.

What am I doing incorrectly?

    <html>
    <body>
    <div id="content">
        <div class="entry">
            <img src="someimagesource.jpg"/>
            <img src="someimagesource123.jpg"/>
        </div>
    </div>
            </body>
            </html>

Upvotes: 0

Views: 799

Answers (1)

ajreal
ajreal

Reputation: 47321

If you are to look for first image, here is the xpath:

image = '//div[@id="content"]//div[@class="entry"]//img[1]/@src';

If you are need the first image (does not need to follow the html structure),

DOMDocument::getElementsByTagName

is more appropriate. refer here : http://www.php.net/manual/en/domdocument.getelementsbytagname.php

Upvotes: 1

Related Questions