0

i need to find the image link and a href . show href,img src, and alt tag.

here my code

$xpath = new DOMXPath('http,://.....');


foreach ($xpath->query('//a[@href]//img') as $img) {


echo '<a href=' .'"' .$img['href'] .'"' .'/>' .'<img src="' .$img['src']    .'"' .'alt="' .$img['alt'] .'"/>' .'</a>';



Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an     
instance of DOMDocument, string given, called in

Can you help me??

vallez
  • 117
  • 1
  • 1
  • 10
  • load `DOMDocument` first, then use `DOMXpath` you can't just dump the url directly – Kevin Jul 12 '16 at 23:40
  • For reference: [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – FirstOne Jul 12 '16 at 23:42

1 Answers1

0

You can use the following code

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://example.com/');

$xpath = new DOMXpath($dom);

then use xpath to do whatever you want to do with it

Edited to get image src from link

# get the images inside a link
foreach ($xpath->query('//a[@href]//img') as $img) {

    # find all the links and images       
    for ($link = $img; $link->tagName !== 'a'; $link = $link->parentNode);

    $output[] = array(
        'href' => $link->getAttribute('href'),
        'src'  => $img->getAttribute('src'),
        'alt'  => $img->getAttribute('alt'),
    );
}
Dharam
  • 423
  • 2
  • 10