我使用Simple HTML DOM来抓取一个页面以获取最新消息,然后使用这个PHP 类生成一个 RSS 提要。
这就是我现在所拥有的:
<?php
// This is a minimum example of using the class
include("FeedWriter.php");
include('simple_html_dom.php');
$html = file_get_html('http://www.website.com');
foreach($html->find('td[width="380"] p table') as $article) {
$item['title'] = $article->find('span.title', 0)->innertext;
$item['description'] = $article->find('.ingress', 0)->innertext;
$item['link'] = $article->find('.lesMer', 0)->href;
$item['pubDate'] = $article->find('span.presseDato', 0)->plaintext;
$articles[] = $item;
}
//Creating an instance of FeedWriter class.
$TestFeed = new FeedWriter(RSS2);
//Use wrapper functions for common channel elements
$TestFeed->setTitle('Testing & Checking the RSS writer class');
$TestFeed->setLink('http://www.ajaxray.com/projects/rss');
$TestFeed->setDescription('This is test of creating a RSS 2.0 feed Universal Feed Writer');
//Image title and link must match with the 'title' and 'link' channel elements for valid RSS 2.0
$TestFeed->setImage('Testing the RSS writer class','http://www.ajaxray.com/projects/rss','http://www.rightbrainsolution.com/images/logo.gif');
foreach($articles as $row) {
//Create an empty FeedItem
$newItem = $TestFeed->createNewItem();
//Add elements to the feed item
$newItem->setTitle($row['title']);
$newItem->setLink($row['link']);
$newItem->setDate($row['pubDate']);
$newItem->setDescription($row['description']);
//Now add the feed item
$TestFeed->addItem($newItem);
}
//OK. Everything is done. Now genarate the feed.
$TestFeed->genarateFeed();
?>
我怎样才能使这段代码更简单?知道有两个 foreach 语句,我该如何组合它们?
因为抓取的新闻是挪威语,所以我需要在标题上应用 html_entity_decode()。我在这里尝试过,但我无法让它工作:
foreach($html->find('td[width="380"] p table') as $article) {
$item['title'] = html_entity_decode($article->find('span.title', 0)->innertext, ENT_NOQUOTES, 'UTF-8');
$item['description'] = "<img src='" . $article->find('img[width="100"]', 0)->src . "'><p>" . $article->find('.ingress', 0)->innertext . "</p>";
$item['link'] = $article->find('.lesMer', 0)->href;
$item['pubDate'] = unix2rssdate(strtotime($article->find('span.presseDato', 0)->plaintext));
$articles[] = $item;
}
谢谢 :)