freeflow Posted September 5, 2012 Share Posted September 5, 2012 Hi everyone. I need some help to change the following code so it uses htmlentities to convert the text from the feeds. The news feeds are in German and some of the letters don't appear correctly in the browser. How do I change the code to make it work? <? $var = @$_GET['z'] ; $var2 = @$_GET['q'] ; echo "<h4> Yahoo! News - $var2</h4>"; $feed_url = "http://$var"; class RSSParser { var $insideitem = false; var $tag = ""; var $title = ""; var $description = ""; var $link = ""; function startElement($parser, $tagName, $attrs) { if ($this->insideitem) { $this->tag = $tagName; } elseif ($tagName == "ITEM") { $this->insideitem = true; } } function endElement($parser, $tagName) { if ($tagName == "ITEM") { print " <b>" . $this->title . "</b> <div class=\"text\">" . $this->description . "<br /><a href=\"" . $this->link . "\" target=\"_blank\"></a></div><br />"; $this->title = ""; $this->description = ""; $this->link = ""; $this->insideitem = false; } } function characterData($parser, $data) { if ($this->insideitem) { switch ($this->tag) { case "TITLE": $this->title .= $data; break; case "DESCRIPTION": $this->description .= $data; break; case "LINK": $this->link .= $data; break; } } } } $xml_parser = xml_parser_create(); $rss_parser = new RSSParser(); xml_set_object($xml_parser,&$rss_parser); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("$feed_url","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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/ Share on other sites More sharing options...
spiderwell Posted September 5, 2012 Share Posted September 5, 2012 would it maybe be the character set declaration is incorrect? can you show exmaples of what is being shown incorrectly Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1375502 Share on other sites More sharing options...
requinix Posted September 5, 2012 Share Posted September 5, 2012 What character encoding is the feed in and what encoding is your site in? Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1375517 Share on other sites More sharing options...
freeflow Posted September 5, 2012 Author Share Posted September 5, 2012 This is the encoding: <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> An example of what is being shown incorrectly (German): Ein schweres Erdbeben der Stärke 7,6 in Costa Rica hat eine Tsunamiwarnung ausgelöst. Laut der US-Erdbebenwarte USGS hatte das Beben eine Stärke von 7,6. Sein Zentrum lag demnach etwa zehn Kilometer östlich der Stadt Hojancha im Nordwesten des mittelamerikanischen Landes. Wrong Stärke ausgelöst östlich Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1375524 Share on other sites More sharing options...
requinix Posted September 5, 2012 Share Posted September 5, 2012 And the feed? Is it in UTF-8 too? Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1375541 Share on other sites More sharing options...
Christian F. Posted September 6, 2012 Share Posted September 6, 2012 I reckon the feed is in UTF-8, seeing as the the result is a typical one-step transition from UTF-8 to ISO-8859-1* This leads me to believe that the web server is not sending the correct header, and thus overriding the UTF-8 declaration in the HTML code itself. Otherwise we'd be seeing the diamond with the question mark. Though, knowing that this forum has some issues with charsets, I cannot be 100% certain that what I'm seeing here is different from the actual reality. Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1375670 Share on other sites More sharing options...
freeflow Posted September 6, 2012 Author Share Posted September 6, 2012 I have used htmlentities in other parts of my website to solve this problem, but I don't know how to implement it in the rss feed. A few days ago the feed didn't work with any browser (Opera/Safari) I used. Today they seem to work with Opera + the mobile version, but not with Safari. I reckon the feed is in UTF-8, seeing as the the result is a typical one-step transition from UTF-8 to ISO-8859-1* This leads me to believe that the web server is not sending the correct header, and thus overriding the UTF-8 declaration in the HTML code itself. Otherwise we'd be seeing the diamond with the question mark. Which server do you mean? The Feed server or mine? Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1375781 Share on other sites More sharing options...
Christian F. Posted September 6, 2012 Share Posted September 6, 2012 Yours. Also, htmlentities () won't really help, as the RSS feed is using XML and not HTML. If it does, then it's more by coincidence, and it is actually more likely to introduce parsing errors. Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1375885 Share on other sites More sharing options...
freeflow Posted September 8, 2012 Author Share Posted September 8, 2012 Thanks a lot for your replies! I guess I'll have to leave it the way it is for now Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1376282 Share on other sites More sharing options...
Christian F. Posted September 8, 2012 Share Posted September 8, 2012 I don't see why, as you can easily modify the headers sent to the correct ones. You only need to know what the correct ones are, which you do in this case. Take a look at the header () function in the PHP manual, should help you along. Quote Link to comment https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/#findComment-1376299 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.