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 に答える