Click to See Complete Forum and Search --> : Special Charachters apearing as #1234;


danpoulton
09-26-2007, 01:49 AM
I'm using the below script to retrieve RSS Feeds for my homepage.


<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$locations = array('http://www.gameslayer.co.uk/blog/feed');
srand((float) microtime() * 10000000); // seed the random gen
$random_key = array_rand($locations);
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>",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($locations[$random_key], '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);
?>


Some charcters appear weird, such as ' is & # 8 2 1 7 ; (added spaces to stop it appearing as '). See for yourself at www.gameslayer.co.uk.

Thanks for any help.

bokeh
09-26-2007, 02:42 AM
There is some internal validation going on that is converting &#8217; into &amp;#8217;

NogDog
09-26-2007, 03:03 AM
My guess is that the endElement() function gets called more than once, and since you are applying htmlspecialchars() to a couple of global variables, on successive calls it is therefore doing the htmlspecialchars() thing to those variables after it's already been done. For instance, if the string variable include a & character:

String value before:
foo & bar
After first call:
foo &amp; bar
After 2nd call:
foo &amp;amp; bar
After 3rd call:
foo &amp;amp;amp; bar
etc....

danpoulton
09-26-2007, 11:20 AM
I know very little about PHP :confused: . How would i edit the code?