Meo
Meo

Reputation: 241

How to get all images on post

at my current website i am using this code to get the first image , witch is inside the post

$first_img = '';
    $my1content = AD($row['post_content']);
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches); 
    $first_img = $matches [1] [0];
    if(empty($first_img)){ //Defines a default image
        $first_img = "/img/default.png";
    }

I wanted to know how to get all the images that are in the post, not only the first one. Thank you for reading this message.

Upvotes: 1

Views: 153

Answers (1)

Myles
Myles

Reputation: 21520

$matches[1] should be an array, iterate over $matches[1] to get all the img tags. This assumes that $my1content has all the content.

for ($matches[1] as $match) {
    //do the stuff you want to do with a match
    $imgUrl = $match[1]; //Do something with this
}

Upvotes: 1

Related Questions