473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

RSS Feed Parsing with Limit

I've set up a news links section using the RSS parsing tutorial found
here: http://www.sitepoint.com/article/560
Everything works great but depending on the feed source there may be 3
news links or 30. How can I limit the amount of titles, descriptions,
and links output to be no more than 10?
Thanks,
Steve.

[MY SLIGHTLY MODIFIED CODE PASTED BELOW]
<dl>
<?php

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$feedurl="http://rss.news.yahoo.com/rss/business";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
$description=str_replace("'","",$description); //take out ' characters
that were causing probs with tooltip javascript
printf("<dt><a href='%s' target='mtgnews' onmouseover=\"return
overlib('%s')\" onmouseout=\"return nd();\">%s</a></dt>",
trim($link),htmlspecialchars(trim($description)),h tmlspecialchars(trim($title)));

$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;

break;
case "LINK":
$link .= $data;
break;
}
}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($feedurl,"r")
or die("Business News Unavailable.");

while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

?>
</dl>
Jul 17 '05 #1
2 3119
Noyb wrote:
I've set up a news links section using the RSS parsing tutorial found
here: http://www.sitepoint.com/article/560
Everything works great but depending on the feed source there may be 3
news links or 30. How can I limit the amount of titles, descriptions,
and links output to be no more than 10?


Try this:

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
static $counter = 0;
if ($name == "ITEM" && !(++$counter > 10)) {
....
JW

Jul 17 '05 #2
use a static counter.
for infor on static varaibles see
http://www.php.net/manual/en/languag...bles.scope.php

lets assume you the number of items you want to limit it to is in
$limit. so in your startElement() function

static $n = 0;

if($n++ < $limit) {
// main function processing goes here.
}

Noyb wrote:
I've set up a news links section using the RSS parsing tutorial found
here: http://www.sitepoint.com/article/560
Everything works great but depending on the feed source there may be 3
news links or 30. How can I limit the amount of titles, descriptions,
and links output to be no more than 10?
Thanks,
Steve.

[MY SLIGHTLY MODIFIED CODE PASTED BELOW]
<dl>
<?php

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$feedurl="http://rss.news.yahoo.com/rss/business";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
$description=str_replace("'","",$description); //take out '
characters that were causing probs with tooltip javascript
printf("<dt><a href='%s' target='mtgnews' onmouseover=\"return
overlib('%s')\" onmouseout=\"return nd();\">%s</a></dt>",
trim($link),htmlspecialchars(trim($description)),h tmlspecialchars(trim($title)));
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;

break;
case "LINK":
$link .= $data;
break;
}
}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($feedurl,"r")
or die("Business News Unavailable.");

while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

?>
</dl>

------------ And now a word from our sponsor ------------------
For a quality usenet news server, try DNEWS, easy to install,
fast, efficient and reliable. For home servers or carrier class
installations with millions of users it will allow you to grow!
---- See http://netwinsite.com/sponsor/sponsor_dnews.htm ----
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Martman | last post by:
I am new to PHP but testing the ability to read an RSS xml feed. I have successfully retrieved, read, and displayed an RSS feed from pcworld. However, when you click on one of the links it adds...
1
by: Chris Mosser | last post by:
I'm looking to add a page to my site where I can get stock quotes for the wired, but more importantly, the wireless web(ie my cell phone). I know how write the needed php and WAP application, I...
4
by: John | last post by:
Hi everyone ! I am a newbie to this newsgroup. I would appreciate if someone can help me to solve this problem. OK, I am currently writing a webpage in PHP that will get RSS file(XML file)...
2
by: Aaron | last post by:
Is there a way to limit the number of rss item rendered by the script. For example the MSDN feed has over 30 items (posts). I would like my script to render only 10 items....
4
by: Florian Lindner | last post by:
Hello, I'm looking for python RSS feed parser library. Feedparser http://feedparser.org/ does not seem to maintained anymore. What alternatives are recommendable? Thanks, Florian
8
by: Steve | last post by:
Can any see why Magpie RSS won't accept this RSS Feed (that is valid according to the Feed Validator website). http://www.westyorksprobation.org.uk/rss.php?type=Library This is the code that...
1
by: =?Utf-8?B?RGlmZmlkZW50?= | last post by:
Hi All, I have created an RSS feed reader. However, the feed that I am trying to read has some invalid characters which my reader does not like. I have no control on the RSS feed but I would...
2
jamwil
by: jamwil | last post by:
What's up guys. I'm having some issues... I've created a method as part of my lifestreaming class which takes an rss feed, and puts the data into a database... It's fairly simple... Check...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.