Connecting Tech Pros Worldwide Forums | Help | Site Map

RSS Feed Parsing with Limit

Noyb
Guest
 
Posts: n/a
#1: Jul 17 '05
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>

Janwillem Borleffs
Guest
 
Posts: n/a
#2: Jul 17 '05

re: RSS Feed Parsing with Limit


Noyb wrote:[color=blue]
> 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?[/color]

Try this:

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


JW



Terence
Guest
 
Posts: n/a
#3: Jul 17 '05

re: RSS Feed Parsing with Limit


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:
[color=blue]
> 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>[/color]
------------ 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 ----
Closed Thread


Similar PHP bytes