1

在我当前的网站上,我正在使用此代码获取第一张图片,女巫在帖子内

$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";
    }

我想知道如何获取帖子中的所有图像,而不仅仅是第一个。感谢您阅读此消息。

4

1 回答 1

1

$matches[1] 应该是一个数组,遍历 $matches[1] 以获取所有 img 标签。这假设 $my1content 包含所有内容。

for ($matches[1] as $match) {
    //do the stuff you want to do with a match
    $imgUrl = $match[1]; //Do something with this
}
于 2011-09-02T22:04:24.497 回答