Jump to content

Recommended Posts

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


?>

Link to comment
https://forums.phpfreaks.com/topic/268027-rss-feed-code-and-htmlentities/
Share on other sites

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     

 

 

 

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.

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?

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.