Search code examples
phpjqueryparsingphpquery

PHPQuery... trying to get all links of all images from page


I am trying to get all links of all images on a given page using PHPQuery. I am using the PHP support syntax of PHPQuery.

This is the code I have so far:

include('phpQuery-onefile.php');

$all = phpQuery::newDocumentFileHTML("http://www.mysite.com", $charset = 'utf-8');

// in theory this gives me all image sources
$images = $all->find('img')->attr('src'); 

// but if I do `echo $images;` what I get is the src to the first image

Out of curiosity I have tried

$images = $all->find('img:first')->attr('src'); 

and

$images = $all->find('img:last')->attr('src'); 

and it prints correctly the first and the last image's addresses, respectively, but how in hell can I get an array of all links?


Solution

  • Within your foreach loop, you need to wrap the $a with a pq().

    For example:

    $all = phpQuery::newDocumentFileHTML("http://www.mysite.com", $charset = 'utf-8');
    
    $imgs = $all['img'];
    
    foreach ($imgs as $img) {
        // Note: $img must be used like "pq($img)"
        echo pq($img)->attr('src');
    }