RSS Feed Parsing with Limit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Noyb

    #1

    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($p arser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    $tag = $name;
    } elseif ($name == "ITEM") {
    $insideitem = true;
    }
    }

    function endElement($par ser, $name) {
    global $insideitem, $tag, $title, $description, $link;
    if ($name == "ITEM") {
    $description=st r_replace("'"," ",$description) ; //take out ' characters
    that were causing probs with tooltip javascript
    printf("<dt><a href='%s' target='mtgnews ' onmouseover=\"r eturn
    overlib('%s')\" onmouseout=\"re turn nd();\">%s</a></dt>",
    trim($link),htm lspecialchars(t rim($descriptio n)),htmlspecial chars(trim($tit le)));

    $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 "DESCRIPTIO N":
    $description .= $data;

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

    $xml_parser = xml_parser_crea te();
    xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
    xml_set_charact er_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("XM L error: %s at line %d",
    xml_error_strin g(xml_get_error _code($xml_pars er)),
    xml_get_current _line_number($x ml_parser)));
    fclose($fp);
    xml_parser_free ($xml_parser);

    ?>
    </dl>
  • Janwillem Borleffs

    #2
    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($par ser, $name) {
    global $insideitem, $tag, $title, $description, $link;
    static $counter = 0;
    if ($name == "ITEM" && !(++$counter > 10)) {
    ....


    JW



    Comment

    • Terence

      #3
      Re: RSS Feed Parsing with Limit

      use a static counter.
      for infor on static varaibles see


      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($p arser, $name, $attrs) {
      > global $insideitem, $tag, $title, $description, $link;
      > if ($insideitem) {
      > $tag = $name;
      > } elseif ($name == "ITEM") {
      > $insideitem = true;
      > }
      > }
      >
      > function endElement($par ser, $name) {
      > global $insideitem, $tag, $title, $description, $link;
      > if ($name == "ITEM") {
      > $description=st r_replace("'"," ",$description) ; //take out '
      > characters that were causing probs with tooltip javascript
      > printf("<dt><a href='%s' target='mtgnews ' onmouseover=\"r eturn
      > overlib('%s')\" onmouseout=\"re turn nd();\">%s</a></dt>",
      > trim($link),htm lspecialchars(t rim($descriptio n)),htmlspecial chars(trim($tit le)));
      >
      >
      > $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 "DESCRIPTIO N":
      > $description .= $data;
      >
      > break;
      > case "LINK":
      > $link .= $data;
      > break;
      > }
      > }
      > }
      >
      > $xml_parser = xml_parser_crea te();
      > xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
      > xml_set_charact er_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("XM L error: %s at line %d",
      > xml_error_strin g(xml_get_error _code($xml_pars er)),
      > xml_get_current _line_number($x ml_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 ----

      Comment

      Working...