Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class BlogPost
- {
- var $date;
- var $ts;
- var $link;
- var $title;
- var $text;
- }
- class BlogFeed
- {
- var $posts = array();
- function BlogFeed($feed_uri)
- {
- $xml_source = file_get_contents($feed_uri);
- $x = simplexml_load_string($xml_source);
- if(count($x) == 0)
- return;
- foreach($x->channel->item as $item)
- {
- $post = new BlogPost();
- $post->date = (string) $item->pubDate;
- $post->ts = strtotime($item->pubDate);
- $post->link = (string) $item->link;
- $post->title = (string) $item->title;
- $post->text = (string) $item->description;
- // Create summary as a shortened body and remove images, extraneous line breaks, etc.
- $summary = $post->text;
- $summary = preg_replace('/<img[^>]*>/i', "", $summary);
- $summary = preg_replace('/^(<br[ ]?\/>)*/i', "", $summary);
- $summary = preg_replace('/(<br[ ]?\/>)*$/i', "", $summary);
- // Truncate summary line to 100 characters
- $max_len = 100;
- if(strlen($summary) > $max_len)
- $summary = substr($summary, 0, $max_len) . '...';
- $post->summary = $summary;
- $this->posts[] = $post;
- }
- }
- }
- $blogFeed = new BlogFeed('http://rss.cnn.com/rss/edition_world.rss');
- $feed = $blogFeed->posts;
- $weHaveElements = false;
- $result = '{ "items": [';
- if (count($feed) > 0) {
- $weHaveElements = true;
- foreach ($feed as $i => $value)
- {
- $result .= '{"id": "'.$i.'", "html": "<a href=\"'.$value->link.'\">'.str_replace('"', '\"', $value->title).'</a><br />'.str_replace('"', '\"', $value->summary).'<br />'.$value->date.'"}, ';
- }
- }
- $result = $weHaveElements ? substr($result, 0, -2) : $result;
- echo $result.' ] }';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment