Search code examples
phpsqlfile-get-contentspreg-match-all

How to get more than one results with preg_match_all and file_get_contents


i'd like to get more than one result using the fuction mentioned in the title, so far i'm just able to get just an only value using preg_match. I'd like to get an array of all the results and then record them in my db.

That's my current code:

$casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast = file_get_contents($casturl);
preg_match_all('|<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="|' , $cast , $castimg );    
print_r($castimg);

When i print the results i'm just giving: Array ( [0] => Array ( [0] =>

I made sure cast contains what i want. I've tried already a lot of possibilities and i got nothing :(


Solution

  • Try this one. I hope you grabbing all image URL's

    $casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
    $cast = file_get_contents($casturl);
    preg_match_all('#<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="#' , $cast , $castimg );    
    print_r($castimg[1]);
    

    Output

    Array ( [0] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/d9NkfCwczP0TjgrjpF94jF67SK8.jpg [1] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/jdRmHrG0TWXGhs4tO6TJNSoL25T.jpg [2] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/gzjmHOSM1vnwnXpU737tdu9YjOu.jpg [3] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/zYiS1KJKEoLstzFA3DV5gwszzSC.jpg [4] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/vDuvdRoliXbjQrptk7GVs4Qj07S.jpg ......)