For Loop without second condition [duplicate] - php

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..

Related

Removing certain things from being scraped

Currently I am scraping this website with the code displayed below but it displays sometimes pages with Mixtape in the title and I am wondering how I can make it skip over these and only crawl the pages that display normally. (demo)
$html = file_get_html('http://beatshype.com/mp3download/');
foreach($html->find('.entry-title a') as $element)
{
print '<br><br>';
echo $url = ''.$element->href;
$html2 = file_get_html($url);
print '<br>';
$image = $html2->find('meta[property=og:image]',0);
print $image = $image->content;
print '<br>';
$title = $html2->find('.single-title',0);
print $title = $title->plaintext;
print '<br>';
$str = explode ("/", $url);
$date = $html2->find('.single-content a',2);
print $date = $date->href;
}
Screenshot:
Top result is good, bottom result is bad.
Very simple, check if the title contains 'mixtape' and go to the next item in the loop:
if(stripos($title->plaintext, 'mixtape') !== false) {
continue;
}
Put that code just before you assign $title to $title->plaintext, or just use $title as the haystack argument.
Some people need it spelled out..
$html = file_get_html('http://beatshype.com/mp3download/');
foreach($html->find('.entry-title a') as $element)
{
$html2 = file_get_html($url);
$title = $html2->find('.single-title',0);
if(stripos($title, 'mixtape') !== false) continue;
$title = $title->plaintext;
print '<br><br>';
echo $url = ''.$element->href;
print '<br>';
$image = $html2->find('meta[property=og:image]',0);
print $image = $image->content;
print $title.'<br>';
$str = explode ("/", $url);
$date = $html2->find('.single-content a',2);
print $date = $date->href;
}
First
print $image = $image->content;
looks superflous.
It both sets $image = $image->content and prints it.
But instead of grabbing and printing each line one after another, grab the title, then decide if you want to fetch the other lines and print the record.
$html = file_get_html('http://beatshype.com/mp3download/');
foreach($html->find('.entry-title a') as $element)
{
$url = ''.$element->href;
$html2 = file_get_html($url);
$title = $html2->find('.single-title',0);
if (strpos($title->plaintext,"MIXTAPE")===FALSE) {
$image = $html2->find('meta[property=og:image]',0);
$date = $html2->find('.single-content a',2);
print '<br><br>';
echo $url;
print '<br>';
print $image->content;
print '<br>';
print $title->plaintext;
print '<br>';
print $date->href;
}
}

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;

How to display RSS Feeds on a website, the other way? [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 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

Limiting the number of feed items displayed

Below is roughly what I am using to display items from a feed. It works fine but the feed has many items and I want to be able to just display the first 5 items in the feed. How can this e done?
<?php
$theurl = 'http://www.theurl.com/feed.xml';
$xml = simplexml_load_file($theurl);
$result = $xml->xpath("/items/item");
foreach ($result as $item) {
$date = $item->date;
$title = $item->title;
echo 'The title is '. $title.' and the date is '. $date .'';
} ?>
foreach ($result as $i => $item) {
if ($i == 5) {
break;
}
echo 'The title is '.$item->title.' and the date is '. $item->date;
}
A for loop may be more suitable for this than a foreach loop:
for ($i=0; $i<=4; $i++) {
echo 'The title is '.$result[$i]->title.' and the date is '. $result[$i]->date;
}
This loop has a much higher performance when not modifying anything in the array, so if speed matters I'd recommend it.
Just do it as part of the XPath query:
<?php
$theurl = 'http://www.theurl.com/feed.xml';
$xml = simplexml_load_file($theurl);
$result = $xml->xpath('/items/item[position() <= 5]');
foreach ($result as $item) {
$date = $item->date;
$title = $item->title;
echo 'The title is '. $title.' and the date is '. $date . '';
}
?>
Here's a demo!

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