SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem reading RSS feeds...

    So i've got this snippet of code that I use to read articles stored in a folder. It rotates them, and serves up the first xx characters.

    The problem I'm running into is that if there is ANY punctuation (, - ' ) in those first xx characters it's throwing up a xml not well formed error.

    Any suggestions?

    PHP Code:
    <?
    error_reporting(0);
    function startElement($parser, $tagName, $attrs) {
            global $insideitem, $tag;
        if ($insideitem) {
            $tag = $tagName;
        } elseif (($tagName == "ITEM") || ($tagName == "IMAGE")) {
            $insideitem = true;
        }
    }
    function endElement($parser, $tagName) {
            global $insideitem, $tag, $title, $description, $link, $iurl, $i, $n;
            if ($i >= $n) {
                    return;
            } 
        if ($tagName == "ITEM") {
            printf("<div align=\"left\">");
            printf("<div class=\"rss_title\"><a href=\"$link\">");
            printf("<strong>");
            printf(htmlspecialchars(trim($title)));
            printf("</strong>");
            printf("</a></div>\n");
            printf("<div class=\"rss_desc\">");
            if (strlen ($description) > 0) {
                printf(trim($description));
                 }
            printf("</div>\n");
            printf("<br />\n");
            printf("</div>");
            $title = "";
            $description = "";
            $link = "";
            $iurl = "";
            $insideitem = false;
            $i += 1;
        }
    }
    function characterData($parser, $data) {
            global $insideitem, $tag, $title, $description, $link, $iurl;
        if ($insideitem) {
        switch ($tag) {
            case "TITLE":
            $title .= $data;
            break;
            case "DESCRIPTION":
            $description .= $data;
            break;
            case "LINK":
            $link .= $data;
            break;
            case "URL":
            $iurl .= $data;
            break;
        }
        }
    }
    function parse_feed ($url, $num) {
            global $insideitem, $tag, $title, $description, $link, $iurl, $i, $n;

    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    $iurl = "";
    $i = 0;
    $n = $num;

    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    $fp = fopen($url,"r")
            or die("Error reading RSS data.");
    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);
    $i = 0;
    }
    ?>

  2. #2
    SitePoint Zealot zealus's Avatar
    Join Date
    Jan 2004
    Location
    NY
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to take a look at this to sanitize input: http://us3.php.net/htmlentities


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •