...

View Full Version : Resolved Using PHP to display RSS Feed - formatting problems.



Stubworth
11-16-2010, 12:03 AM
Hey all.

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


<?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! :)

MattF
11-16-2010, 12:47 AM
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

mlseim
11-16-2010, 01:19 AM
Yes ... make it simple ...

Try this:


<?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 />";
}

?>





.

Stubworth
11-16-2010, 06:35 PM
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?

mlseim
11-16-2010, 08:56 PM
You have to pick the tags you want ... like this.




<?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.




.

Stubworth
11-16-2010, 11:13 PM
Thanks for your help (official 'Thank' given).

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

:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum