How to display RSS Feeds on a website, the other way? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to display RSS feeds of a website on my website. This could be easily done by online services like: www.rssinclude.com, www.feedgrabbr.com, etc. But I want the full code... right from the beginning.

For my soccer news website, I use the following code: (Hope, it works)
<?php
class rss
{
var $feed;
function rss($feed)
{
$this->feed = $feed;
}
function parse()
{
$rss = simplexml_load_file($this->feed);
$rss_split = array();
foreach ($rss->channel->item as $item) {
$title = (string) $item->title;
$link = (string) $item->link;
$description = (string) $item->description;
$rss_split[] = '<div>
'.$title.'
<hr>
</div>';
}
return $rss_split;
}
function display($numrows,$head)
{
$rss_split = $this->parse();
$i = 0;
$rss_data = '<div class="container">
<div class="title">'.$head.'</div>
<div class="links">';
while ( $i < $numrows )
{
$rss_data .= $rss_split[$i];
$i++;
}
$trim = str_replace('', '',$this->feed);
$user = str_replace('&lang=en-us&format=rss_200','',$trim);
$rss_data.='</div></div>';
return $rss_data;
}
}
$feedlist = new rss("http://www.fifa.com/rss/index.xml");
echo $feedlist->display(10,"FIFA");
?>

Try Simple XML
<?php
$html = "";
$url = "file.rss";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$pubDate = $xml->channel->item[$i]->pubDate;
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= "$description";
$html .= "<br />$pubDate<hr />";
}
echo $html;
?>
Short Video Tutorial - Youtube

Related

Find missing values in array - PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array counting multiple values like D0001, D0002, D0003, ...
But sometimes a value is missing, in this case these should be the new ID. If there is no value missing then take the next one in line.
This is the code I used to retrieve an array.
example => array = array("D0001","D0002","D0003","DOOO5");
# function finding missing values?
function missing_values($list)
$range = 0;
foreach ($list as $l){
$nr = $range+1
if ($l != $nr){
$id = $nr;
}
}
$numbers = array();
while($row = $result->fetch_array()){
$id = $row['id'];
echo "--------------------------------" . "<br/>";
echo "ID " . " --> " . $id ."<br/>";
$id = substr($id,1);
echo "ID " . " --> " . $id ."<br/>";
$id = ltrim($id, '0');
echo "ID " . " --> " . $id ."<br/>";
$list[] = $id;
}
print_r($list);
# Find missing values
$array = missing_values($list);
if(empty($array)){
$max = max($list);
$id = $max+1;
}else{
$keys = array_keys($array);
$id = $array[$keys[0]];
}
Could someone tell me how I can't find the missing values in array? Or are the more easy ways to do this?
# Determine the first free id
function get_first_free_id($id_list){
// construct a new array
$new_array = range($id_list[0],max($id_list));
// use array_diff to find the missing elements
$mis_array = array_diff($new_array, $id_list);
$keys = array_keys($mis_array);
if(empty($keys)){
$highest = max($id_array);
$id = $highest+1;
}else{
$id = $mis_array[$keys[0]];
}
return $id;
}
You can do this using the bellow logic
$myArray = array("D0001","D0002","D0003","D0005", "D0009", "D0019", "D0020", "D0030");
$first = reset($myArray);
$last = end($myArray);
for ($i=$first; $i<=$last; $i++) {
$index = array_search($i, $myArray);
if(!is_numeric($index)) {
echo "missing " .$i;
echo "<br>";
}
}
http://sandbox.onlinephpfunctions.com/code/25daa236e6f02a0566dfe0ea06124776dbf20da0

For Loop without second condition [duplicate]

This question already has answers here:
Loop a multidimensional array and only print two specific column values per row
(6 answers)
Closed 5 years ago.
<?php
$val = $_GET["val"];
$url = "http://feeds.bbci.co.uk/news/rss.xml";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10 ; $i++){
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$pubDate = $xml->channel->item[$i]->pubDate;
$rss .= "<a href='$link'><h3>$title</h3></a>";
$rss .= "$description";
$rss .= "<br />$pubDate<hr />";
}
echo $rss;
?>
Hello everyone! I have a problem here. I would like to list all the results for the rss link but it gets only 10. I know that I have the second condition for "for loop" as $i<10, but how can I remove that condition, and get all the results from the rss link?
Use foreach instead of for:
<?php
$url = "http://feeds.bbci.co.uk/news/rss.xml";
$xml = simplexml_load_file($url);
$rss = '';
foreach ($xml->channel->item as $item) {
$title = $item->title;
$link = $item->link;
$description = $item->description;
$pubDate = $item->pubDate;
$rss .= "<a href='$link'><h3>$title</h3></a>";
$rss .= "$description";
$rss .= "<br />$pubDate<hr />";
}
echo $rss;
?>
Either you can use foreach loop or count the size of an array and then use this size to set the second condition in your for loop..

How to get first <p> from each of <description> in an XML file?

I'm parsing a RSS feed to get the raw data and manipulate it.
On a WordPress RSS feed. I can find the title, link, description and publication of a the post by iterating over the SimpleXMLElement. The nodes are located in:
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$pubDate = $xml->channel->item[$i]->pubDate;
respectively.
The problem is $description had 2 <p>s inside. One one which is useless for me; the second one.
So how do I assign $description to only the first <p> of description?
Getting simply $xml->channel->item[$i]->description->p[0] won't work. It results in an internal server error.
My whole code looks like this:
<?php
$html = "";
$url = "http://sntsh.com/posts/feed/";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description->children();
$pubDate = $xml->channel->item[$i]->pubDate;
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= "$description";
$html .= "<br />$pubDate";
}
echo $html;
You can get the children of an element using the children() method. If you can guarantee that the first child will always be the element that you need, you can use it this way:
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description->children();
$pubDate = $xml->channel->item[$i]->pubDate;
The children() function is meant to be used in an iterative manner, where every time you call it it returns the next child as a SimpleXMLElement. http://php.net/manual/en/simplexmlelement.children.php
Edit
It seems that the cause of the issue are the <![CDATA[ ]]> tags. They cause the SimpleXMLElement to be empty. Stripping them fixes it:
$html = '';
$src = file_get_contents('http://sntsh.com/posts/feed/');
$search = ["<![CDATA[","]]>"];
$replace = array('','');
$data = str_replace($search,$replace,$src);
$xml = simplexml_load_string($data);
for($i = 0; $i < count($xml->channel->item); $i++)
{
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description->children();
// Or
// $description = $xml->channel->item[$i]->description->p[0];
$pubDate = $xml->channel->item[$i]->pubDate;
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= trim($description).'...';
$html .= "<br />$pubDate";
}
echo $html;

Displaying Blogs according to publish time

I have a doubt am displaying posts of blogs[more than 1] and now i want to display blogs according to publish date mean new post 1st next 2nd and so on...
MY CODE
require_once('rss_fetch.inc');
$dateArray= "";
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
'http://raghuks.wordpress.com/feed'
);
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
$dateArray=array();
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 1) break;
}
}
Only recent 4 posts should display from any blog but that should be according to date
Please help..
What i tried is putting all date into an array and using bubble sort but its not working.. Please Help Me..
Thanks In Advance
require_once('rss_fetch.inc');
$dateArray= "";
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
'http://raghuks.wordpress.com/feed'
);
$result_array = array();
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
$dateArray=array();
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
$tm = strtotime($date);
$result_array[$tm]['title'] = $title;
$result_array[$tm]['url'] = $url;
$result_array[$tm]['desc'] = $desc;
$result_array[$tm]['date'] = $date;
if($i == 1) break;
}
ksort($result_array);
foreach($result_array as $result)
{
echo "<div class=\"blog\"><a target=\"_blank\" href=$result['url']><h1>$result['title']</h1>$result['desc']<br/><br/>DATED : $result['date'] <br/><br/></a></div> ";
}
}

About Limiting Rss Feed

I want only the latest 5 feeds to be shown on my website.
I am using the following code to fetch rss feed... Can any one help to limited feeds to be shown... Thank You In ADVANCE :)
CODE THAT AM USING
<?php
require_once('rss_fetch.inc');
$url = 'http://news.google.com/news?ned=us&topic=h&output=rss';
$rss = fetch_rss($url);
echo "Site: ", $rss->channel['title'], "<br>\n";
foreach ($rss->items as $item ) {
$title = $item['title'];
$url = $item['link'];
$desc = $item['description'];
$category = $item['category'];
echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> ";
}
?>
Limit it using foreach?
foreach ($rss->items as $i => $item ) { // use $i as counter
$title = $item['title'];
$url = $item['link'];
$desc = $item['description'];
$category = $item['category'];
echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> ";
if($i == 4) break; // add this, == 4 is because $i starts from 0
}
If you're looking to limit the number of posts, you just need to keep track of them and break out of the foreach loop when applicable, e.g.
<?php
require_once('rss_fetch.inc');
$url = 'http://news.google.com/news?ned=us&topic=h&output=rss';
$rss = fetch_rss($url);
echo "Site: ", $rss->channel['title'], "<br>\n";
$numposts = 0;
$maxposts = 5;
foreach ($rss->items as $item ) {
$numposts++;
if ($numposts<=$maxposts) {
$title = $item['title'];
$url = $item['link'];
$desc = $item['description'];
$category = $item['category'];
echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> ";
} else {
break;
}
}
?>

Categories