I'm parsing a XML feed using PHP, but when the HTML is outputed. Title data and Description data will omit information before the apostrophe symbol (') . For example if one of the titles of the RSS feed is "Gas prices shouldn't go up" It'll output as "t go up" leaving out Gas prices shouldn, you can see more @ www.gimme5ive.com/ent/index.php . Here is my PHP code:
<?php
class xItem {
var $xTitle;
var $xLink;
var $xDescription;
}
// general vars
$sPubDate = "";
$sUrl = "";
$sTitle = "";
$sLink = "";
$sDescription = "";
$arItems = array();
$itemCount = 0;
// ********* Start User-Defined Vars ************
// rss url goes here
$uFile = "http://www.sohh.com/syndication/rss/daily_hip_hop_news_2_0.rss";
// descriptions (true or false) goes here
$bDesc = true;
// font goes here
$uFont = "Verdana, Arial, Helvetica, sans-serif";
$uFontSize = "1";
// ********* End User-Defined Vars **************
function startElement($parser, $name, $attrs) {
global $curTag;
$curTag .= "^$name";
}
function endElement($parser, $name) {
global $curTag;
$caret_pos = strrpos($curTag,'^');
$curTag = substr($curTag,0,$caret_pos);
}
function characterData($parser, $data) { global $curTag; // get the Channel information first
global $sPubDate, $sUrl, $sTitle, $sLink, $sDescription;
$pubDateKey = "^RSS^CHANNEL^PUBDATE";
$urlKey = "^RSS^CHANNEL^IMAGE^URL";
$titleKey = "^RSS^CHANNEL^TITLE";
$linkKey = "^RSS^CHANNEL^LINK";
$descKey = "^RSS^CHANNEL^DESCRIPTION";
if ($curTag == $pubDateKey) {
$sPubDate .= $data;
}
elseif ($curTag == $urlKey) {
$sUrl .= $data;
}
elseif ($curTag == $titleKey) {
$sTitle .= $data;
}
elseif ($curTag == $linkKey) {
$sLink .= $data;
}
elseif ($curTag == $descKey) {
$sDescription .= $data;
}
// now get the items
global $arItems, $itemCount;
$itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE";
$itemLinkKey = "^RSS^CHANNEL^ITEM^LINK";
$itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION";
if ($curTag == $itemTitleKey) {
// make new xItem
$arItems[$itemCount] = new xItem();
// set new item object's properties
$arItems[$itemCount]->xTitle .= $data;
}
elseif ($curTag == $itemLinkKey) {
$arItems[$itemCount]->xLink .= $data;
}
elseif ($curTag == $itemDescKey) {
$arItems[$itemCount]->xDescription .= $data;
// increment item counter
$itemCount++;
}
}
// main loop
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("could not open RSS for input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
// write out the items
?>
<a href = "<?php echo($sLink); ?>"><img border= "0" src = "<?php echo($sUrl); ?>"></a>
<br>
<b>Updated: <?php echo($sPubDate); ?></b>
<br>
<?php
for ($i=0;$i<count($arItems);$i++) {
$txItem = $arItems[$i];
?>
<table width="85%" border="0" cellspacing="0" cellpadding="0">
<tr><a target="_blank" href = "<?php echo ($txItem->xLink); ?>"><?php echo trim($txItem->xTitle); ?></a></tr>
<?php
if ($bDesc) {
?>
<tr><?php echo trim($txItem->xDescription); ?></tr></table>
<?php
}
}
?>