dongzhuohan7085 2011-10-04 19:54
浏览 53
已采纳

简单的RSS编码问题

Consider the following PHP code for getting RSS news on a site I'm developing:

<?php
$url = "http://dariknews.bg/rss.php";
$xml = simplexml_load_file($url);

$feed_title = $xml->channel->title;
$feed_description = $xml->channel->description;
$feed_link = $xml->channel->link;
$item = $xml->channel->item;

    function getTheData($item){
        for ($i = 0; $i < 4; $i++) {
        $article_title = $item[$i]->title;
        $article_description = $item[$i]->description;
        $article_link = $item[$i]->link;
        echo "<p><h3><a href=".$article_link.">". $article_title. "</a></h3></p><small>".$article_description."</small><p>";
        }
    }
?>

The data accumulated by this function should be presented in the following HTML format:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Новини от Дарик</title>
  </head>
   <body>
    <?php getTheData($item);?>
   </body>
</html>

As you see I added windows-1251(cyrillic) and utf-8 encoding but the RSS feed is unreadable if I don't change the browser encoding to utf-8. The default encoding in my case is cyrilic but I get unreadable feed. Any help making this RSS readable in cyrilic(it's from Bulgaria) will be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • doudou_3636 2011-10-04 20:13
    关注

    I've just tested your code and the Bulgarian characters displayed fine when I removed the charset=windows-1251 meta tag and just left the UTF-8 one. Want to try that and see if it works?

    Also, you might want to change your <html> tag to reflect the fact that your page is in Bulgarian like this: <html xmlns="http://www.w3.org/1999/xhtml" lang="bg" xml:lang="bg">

    Or maybe you need to force the web server to send the content as UTF-8 by sending a Content-Type header:

    <?php
    header("Content-Type: text/html; charset=UTF-8");
    ?>
    

    Just be sure to include this before ANY other content (even whitespace) is sent to the browser. If you don't you'll get the PHP "headers already sent" error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题