0

他の開発者が、RSS フィードの URL からサイトのブログのメイン コンテンツの最初の画像を適切に取得/抽出する方法を知りたいです。RSS フィードには投稿/ブログ アイテムの画像 URL がないため、これは私が考える方法です。見続けているのに

<img src="http://feeds.feedburner.com/~r/CookingLight/EatingSmart/~4/sIG3nePOu-c" />

ただし、1px の画像のみです。これはフィード アイテムに関連する値を持っていますか、それとも実際の画像に変換できますか? RSS http://feeds.cookinglight.com/CookingLight/EatingSmart?format=xmlはこちら

とにかく、フィード内の URL を使用して画像を抽出しようとする試みは次のとおりです。

function extact_first_image( $url ) {  
  $content = file_get_contents($url);

  // Narrow the html to get the main div with the blog content only.
  // source: http://stackoverflow.com/questions/15643710/php-get-a-div-from-page-x
  $PreMain = explode('<div id="main-content"', $content);
  $main = explode("</div>" , $PreMain[1] );

  // Regex that finds matches with img tags.
  $output = preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $main[12], $matches);  

  // Return the img in html format.
  return $matches[0][0];  
}

$url = 'http://www.cookinglight.com/eating-smart/nutrition-101/foods-that-fight-fat'; //Sample URL from the feed.
echo extact_first_image($url);

<div id="main-content"この関数の明らかな欠点:が html で見つかった場合、適切に分解されます。別の構造で解析する別の xml がある場合、そのための別の展開もあります。それは非常に静的です。

言及する価値があるのは、ロード時間に関するものだと思います。フィード内のアイテムをループして実行すると、さらに長くなります。

ポイントを明確にしていただければ幸いです。おそらくソリューションを最適化するのに役立つ可能性のあるアイデアを自由にドロップしてください.

4

1 に答える 1