Help us understand the problem. What is going on with this article?

RSS1.0とRSS2.0で記事を取得するコード

More than 5 years have passed since last update.

PHPでRSS1.0、RSS2.0で記事を取得する方法です。
以下のコードで、タイトル、URL、投稿日(タイムスタンプ)がとれます。

RSS1.0

    $rss = simplexml_load_file("RSS1.0のURL");
    $array = array();
    foreach ($rss->item as $item) {
        $dc = $item->children('http://purl.org/dc/elements/1.1/');
        $array[] = array('title'=>$item -> title,'url' => $item -> link, 'date' => strtotime($dc->date));

    }

RSS2.0

    $rss = simplexml_load_file("RSS2.0のURL");
    $array = array();
    foreach ($rss->channel->item as $item) {
            $array[] = array('title'=>$item -> title,'url' => $item -> link,'date' => strtotime($item -> pubDate));
    }

アメブロ

アメブロではRSSの間に自動的に広告が組み込まれます。
なので、上記のRSS2.0のコードで記事を取得しようとすると
広告も取得してしまいます。

幸いなことに広告のタイトルには必ずPRという文字が入るという規則性があるので、
PRが含まれていなければ配列に追加するという風に修正しています。

広告じゃなくても、
タイトルにPRが含まれていたら弾かれてしまうのが難点です。

    $rss = simplexml_load_file("アメブロのRSSのURL");
    $array = array();
    foreach ($rss->channel->item as $item) {
        if(!strstr($item -> title,'PR')){//タイトルにPR(広告)が含まれていなければ配列に追加
            $array[] = array('title'=>$item -> title,'url' => $item -> link,'date' => strtotime($item -> pubDate));
        }
    }
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away