doutuo3935 2015-06-23 09:43
浏览 17
已采纳

为什么编码错误?

I am trying to create rss feed. I use yii framework.

Here is my partial view

<?php
header("Content-Type: application/xml; charset=utf-8");
?>
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

    <channel>
        <title>Newskeeper</title>
        <link>newskeeper.ru</link>
        <description>news</description>

        <?php
        $HOST = "http://newskeeper.ru/";
        $HOST_NEWS = "news#news_";
        $IMAGE_PATH = $HOST . "news/sendshow?id=";

        foreach($news as $item) {
            $date=date("D, j M Y G:i:s", $item['create_time']). " GMT";

            echo '<item>';
                echo '<title>' . utf8_encode(html_entity_decode($item['teaser_ru']))  . '</title>';
                echo '<link>' . utf8_encode(html_entity_decode($HOST .$HOST_NEWS . $item['slug'])) . '</link>';
                echo '<description>' .utf8_encode(html_entity_decode($item['text_ru'])). '</description>';
            echo '</item>';
        }
        ?>

    </channel>
</rss>

and here is the method in a controller class

public function actionRss() {
    $sql = "select id, teaser_ru, text_ru, slug, create_time from news order by create_time desc limit 10";
    $command = Yii::app()->db->createCommand($sql);
    $news = $command->queryAll();
    $this->renderPartial("_rss", array('news' => $news));
}

The question is why does not it show russian letter. It shows them in wrong encoding.

  • 写回答

1条回答 默认 最新

  • dptsivmg82908 2015-06-23 09:59
    关注

    I found the issue. I overencoded it. You need to remove utf8_encode and add second and third param to html_entity_decode, so the item of rss feed should look like this

    echo '<item>';
         echo '<title>' . html_entity_decode($item['teaser_ru'], ENT_COMPAT, 'utf-8')  . '</title>';
         echo '<link>' . html_entity_decode($HOST .$HOST_NEWS . $item['slug'], ENT_COMPAT, 'utf-8') . '</link>';
         echo '<description>' . html_entity_decode($item['text_ru'], ENT_COMPAT, 'utf-8') . '</description>';
    echo '</item>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)