PDA

View Full Version : xml/rss reader help


dracule
03-14-2006, 10:51 PM
i found this nice little rss reader online but i would like to modify it so it would only get a snippet of what the article was. like so: if an article is longer than 200 characters, stop it at 200 and put an elipse(...) at the end. I would also like to remove images from it.

Code:
<?
// RSS NEWS READER
// Simple RSS Reader to grab news from whichever site you choose from a select box
// I've included a few popular RSS News Feeds, but add your own using the listbox
// Easy As.
?>

</p>
<form name="form1" method="post" action="<?php echo $PHP_SELF ?>">
<select name="backend" id="backend">
<option value="">BBC NEWS</option>
<option value="http://rss.cnn.com/rss/cnn_world.rss">CNN WORLD NEWS</option>
<option value="http://www.microsite.reuters.com/rss/topNews">REUTERS NEWS</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form>
<p>&nbsp;</p>
<?php
// Variables Needed (some may not be but oh well)
$backend = $_POST['backend'];
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
// end

function startElement($parser, $tagName, $attrs) {

// The function used when an element is encountered

global $insideitem, $tag;

if ($insideitem) {

$tag = $tagName;

} elseif ($tagName == "ITEM") {

$insideitem = true;
}

}

function characterData($parser, $data) {

// The function used to parse all other data than tags

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;
}

}

}

function endElement($parser, $tagName) {

// This function is used when an end-tag is encountered.

global $insideitem, $tag, $title, $description, $link;

if ($tagName == "ITEM") {
echo "<table width=\"100%\" border=\"0\"><tr>
<td class=\"main-table\">
<span class=\"nav-head\"><strong>
";
printf("<p><b><a href='%s'>%s</a></b></p>", // make our title into an actual link
trim($link),htmlspecialchars(trim($title))); // remove html characters from the title

echo "</tr>";
echo "<tr>
<td class=\"nav\">";
printf("<p>%s</p>",$description); // Print out the live journal entry
echo"</td>";
echo "</tr>";
echo "</table>";
echo "<br>";
$title = $description = $link = $insideitem = false;

}

}

// Now to the parsing itself. Starts by creating it:

$xml_parser = xml_parser_create();

// Then setup the handlers:

xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// Open the actual datafile:

$fp = fopen($backend, r);

// Run through it line by line and parse:

while ($data = fread($fp, 100)) {
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)));
}

// Close the datafile

fclose($fp);

// Free any memmory used

xml_parser_free($xml_parser);

?>

kehers
03-15-2006, 02:19 PM
What about trying the free php script for parsing rss around here: http://www.wirelessdevnet.com/channels/wap/features/xmlcast_php.html

ns1987
03-15-2006, 02:21 PM
(Apply this to the code as you see fit, didn't look at it.)

To remove images:
$str = preg_replace('#\<img(.+?)src=\"(.+?)\"(.+?)\>#i', '', $str); //Tell me if this doesn't work, I might have to fool around with the regex a bit.

To do the 200 char thing:
if(strlen($str) > 200))
{
$str = substr($str, 0, 200) . '...';
}
There's a more efficient way for the latter, but i forget. :(

dracule
03-16-2006, 12:43 AM
Ok thanks. now the problem is that it reads the images as characters, so instead of 200 characters being present i get this:([x] symbolizes a broken link to a picture)

[x]In the n...

so im just wondering, what else to i have to change to that code? my images are under the description global so i replaced that, but it still doesnt work. i might try to set up an image for a str and just not display them, like a filter, respond to this code please and if my image filter works ill get back to you as quick as i can.
$description = preg_replace('#\<img(.+?)src=\"(.+?)\"(.+?)\>#i', '', $description);

dracule
03-16-2006, 01:36 AM
I figured out why it was cutting off so much text. here is a snippet where an image is shown from the XML.

<table width="700"><tr><td><img onclick="window.open('/uploads/articles_module/10787_filter_small.jpg')" style="cursor: pointer;" alt="" src="/uploads/articles_module/10787_filter_small_qjgenth.jpg" align="left" border="0">Right here's the final release trailer for Sony's amazing looking action stealth actioner, Syphon Filter: Dark Mirror.

it reads all the stuff in "<>" as characters too. i tried to set it up so that it would stop reading when it hit "<img" and started when it hit > but then it stopped reading the whole article. any help would be much appreciated to solve this little delay. its probably something simple but i just cant find it:mad:

dracule
03-18-2006, 05:04 PM
hello anyone? need help with images.

ralph l mayo
03-19-2006, 06:02 PM
Are you just looking for a way to get rid of the html tags so you can count the visible characters? If so, it's a simple regex:


$sampletext = '<table width="700"><tr><td><img onclick="window.open(\'/uploads/articles_module/10787_filter_small.jpg\')" style="cursor: pointer;" alt="" src="/uploads/articles_module/10787_filter_small_qjgenth.jpg" align="left" border="0">Right here\'s the final release trailer for Sony\'s amazing looking action stealth actioner, Syphon Filter: Dark Mirror.';
$sampletext = preg_replace('/<.+?>/', '', $sampletext);
echo $sampletext; // Right here's the final release trailer for Sony's amazing looking action stealth actioner, Syphon Filter: Dark Mirror.

marek_mar
03-19-2006, 08:11 PM
strip_tags() (http://www.php.net/strip_tags)?

ralph l mayo
03-19-2006, 08:32 PM
strip_tags() (http://www.php.net/strip_tags)?
smart *** :D