Scroll to top

Find Nth Image in HTML

Using below function you can find Nth Image in your HTML Code

function getImageInHTML($post, $default, $i = 0)
    preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post, $matches);
    if( isset($matches[1][$i]) && !empty( $matches[1][$i] )){return $matches[1][$i];}
    if(isset($matches[1][0]) && !empty( $matches[1][0] )){return $matches[1][0];}
    return $default;
Author avatar

Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Post a Comment

Your email address will not be published. Required fields are marked *