Here you have my code:
<?php
$url = "http://feeds.hipertextual.com/alt1040";
$rss = simplexml_load_file($url);
if($rss) {
$items = $rss->channel->item;
foreach($items as $item) {
$title = $item -> title;
$link = $item -> link;
$description = $item -> description;
$replace = preg_replace("/<img[^>]+\>/i", "", $description);
echo utf8_decode("<h3><a href=$link>$title</a></h3>");
echo utf8_decode("<p>$replace</p>");
}
}
?>
I get the RSS from that URL and I parse it so that images don't appear. Until here OK. But now, I want only to be shown the first piece of news from the RSS Feed, not all the news.
If I make a count, it tells there are 25 news items.
$count = count ($items);
echo $count; //25 news...
How can I do to be only shown the first piece of news?
You can insert any number instead of 1 item in this example.
<?php
$url = "http://feeds.hipertextual.com/alt1040";
$rss = simplexml_load_file($url);
if($rss) {
$items = $rss->channel->item;
$count =0;
foreach($items as $item) {
if ($count<1) {
$title = $item -> title;
$link = $item -> link;
$description = $item -> description;
$replace = preg_replace("/<img[^>]+\>/i", "", $description);
echo utf8_decode("<h3><a href=$link>$title</a></h3>");
echo utf8_decode("<p>$replace</p>");
}
$count++;
}
}
?>
why dont you just try to NOT echo the description part?
$url = "http://feeds.hipertextual.com/alt1040";
$rss = simplexml_load_file($url);
if($rss) {
$items = $rss->channel->item;
$isNewsPage = true; // set here to false if you are on main page
foreach($items as $item) {
$title = $item -> title;
$link = $item -> link;
$description = $item -> description;
$replace = preg_replace("/<img[^>]+\>/i", "", $description);
echo utf8_decode("<h3><a href=$link>$title</a></h3>");
if($isNewsPage)
echo utf8_decode("<p>$replace</p>");
}
If your only looking to display the first item, then just set the $item
variable to the first item array. Then you can skip the entire foreach:
<?php
$url = "http://feeds.hipertextual.com/alt1040";
$rss = simplexml_load_file($url);
if($rss) {
$item = $rss->channel->item[0];
$title = $item -> title;
$link = $item -> link;
$description = $item -> description;
$replace = preg_replace("/<img[^>]+\>/i", "", $description);
echo utf8_decode("<h3><a href=$link>$title</a></h3>");
echo utf8_decode("<p>$replace</p>");
}?>