Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    43
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Using PHP to display RSS Feed - formatting problems.

    Hey all.

    I'm using the following code to display an RSS feed:

    Code:
    <?php
    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    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") {
      printf("<dt><b><a href='%s'>%s</a></b></dt>",
      trim($link),htmlspecialchars(trim($title)));
      printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description)));
      $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("http://www.thefreedictionary.com/_/WoD/rss.aspx","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);
    ?>
    The feed displays fine, and I can format it to some extent using <div>'s etc.

    But in the text of the feed, it displays HTML tags, such as <a href="... and <br />.

    Any idea how to work around this?

    Document has a defined DOCTYPE etc. & this problem occurs across all browsers.

    The feed I'm trying to use is found at:

    http://www.thefreedictionary.com/_/WoD/rss.aspx

    If that is any help!

    Any ideas would be great!
    Last edited by Stubworth; 11-16-2010 at 11:13 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Have you thought of trying SimplePie? It'll probably save you a lot of hassle over trying to get your own parser spot on for different feeds.

    http://simplepie.org

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Yes ... make it simple ...

    Try this:
    PHP Code:
    <?php

    $feedurl
    ="http://www.thefreedictionary.com/_/WoD/rss.aspx";

    $xml=simplexml_load_file($feedurl);

    foreach(
    $xml->channel->item as $item){
    echo
    "{$item->description}<br /><br />";  


    ?>



    .
    Last edited by mlseim; 11-16-2010 at 01:21 AM.

  • #4
    New Coder
    Join Date
    Nov 2010
    Posts
    43
    Thanks
    4
    Thanked 2 Times in 2 Posts
    The code you suggested hides all of the HTML tags, however I've now lost the titles of the feeds, i.e. the word which the feed goes on to define.

    It reads:
    DEFINITION: (adjective) Creating an unfavorable or neutral first impression.
    SYNONYMS: unpresentable.
    USAGE: We found the tastiest tapas in the most unprepossessing bars.
    Discuss
    and it should read:
    unprepossessing
    16 November 2010 05:00
    DEFINITION: (adjective) Creating an unfavorable or neutral first impression.
    SYNONYMS: unpresentable.
    USAGE: We found the tastiest tapas in the most unprepossessing bars.
    Discuss
    Any ideas?

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    You have to pick the tags you want ... like this.


    PHP Code:
    <?php

    $feedurl
    ="http://www.thefreedictionary.com/_/WoD/rss.aspx";

    $xml=simplexml_load_file($feedurl);

    foreach(
    $xml->channel->item as $item){
    echo
    "<b>{$item->title}</b><br />";
    echo
    "{$item->description}<br /><br />";  


    ?>
    I would have to play around with it later tonight.




    .

  • Users who have thanked mlseim for this post:

    Stubworth (11-16-2010)

  • #6
    New Coder
    Join Date
    Nov 2010
    Posts
    43
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Thanks for your help (official 'Thank' given).

    However I just decided to use a free widget to the same end.



  •  

    Posting Permissions

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