4

So I have a small PHP script that generates an xml feel for the rss feed of my blog. However, it throws up this error:

This page contains the following errors:

error on line 23 at column 14: Encoding error
Below is a rendering of the page up to the first error.

(seen here: http://aviatex14.co.uk/rss.xml)

Here's the code that generates it:

while ($line = mysql_fetch_array($result)) {
  $return[] = $line;
  var_dump(date("Y-m-d H:i:s", $line['timestamp']));
}

$now = date("D, d M Y H:i:s T");

$output = "<?xml version=\"1.0\" encoding=\"UTF-16\" ?>
             <rss version=\"2.0\">
                <channel>
                  <title></title>
                  <link></link>
                  <description></description>
                  <pubDate></pubDate>
                  <language>en-us</language>
                    
                  <lastBuildDate>$now</lastBuildDate>
          ";
            
foreach ($return as $line) {
  $output .= "<item><title>".htmlentities($line['title'])."</title>
                <link>http://blog.aviatex14.co.uk/permalink.php?uid=".htmlentities($line['uid'])."</link>     
                <description>".htmlentities(strip_tags($line['entry']))."</description>
                <pubDate>".$date = date("Y-m-d H:i:s T", $line['timestamp'])."</pubDate> 
              </item>";
}
$output .= "</channel></rss>";
print "Content-Type: application/rss+xml";
echo $output;

$f = fopen("rss.xml", "w"); 
fwrite($f, $output); 
fclose($f); 

Any help would be appreciated! :D

6
  • I wonder for what htmlentities is used everywhere therein. Anyway, you should provide a charset to make it work for itself at least: php.net/manual/en/function.htmlentities.php Commented Jun 20, 2011 at 19:52
  • encoding=\"UTF-16\" - is this really the output's true encoding? Commented Jun 20, 2011 at 19:53
  • There is no error at that line anymore. Apparently the OP changed the feed after asking. Voting to close. Commented Jun 20, 2011 at 20:01
  • I did, but then it threw up this: "error on line 33 at column 157: Opening and ending tag mismatch: p line 0 and description" Commented Jun 20, 2011 at 20:04
  • Feed items containing HTML have to be either html encoded or put in CDATA tags. Commented Jun 20, 2011 at 20:06

2 Answers 2

6

It says "TOKYO � A Japanese" at that line (and further down in the feed as well). The � is not utf-8. Try to utf8_encode (or iconv if you want a different encoding) the content or even better: use an XML processor to create the feed.

Sign up to request clarification or add additional context in comments.

3 Comments

@hakre then the OP changed that while I answered. When I downloaded the feed it was definitely utf8. I've added iconv as an additional solution.
Works, to the extent of giving me this problem... error on line 33 at column 157: Opening and ending tag mismatch: p line 0 and description .... I'd rather not have to go back and fix the tags :(
This worked though, <description>".iconv("ISO-8859-1", "UTF-8", $line['entry'])."</description>
1

You have to configure Your database or connection. Try to execute mysql_set_charset('utf8'); after connection to database.

http://php.net/manual/en/function.mysql-set-charset.php

P.S. You should use <?xml version="1.0" encoding="UTF-8" ?> or drop that line. Your output looks like 8 bits based.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.