简体   繁体   English

来自MySQL的RSS-特殊字符问题

[英]RSS from MySQL - issue with special characters

<?php
    header('Content-type: text/xml; charset=utf-8');
    DEFINE ('DB_USER', 'root');   
    DEFINE ('DB_PASSWORD', '');   
    DEFINE ('DB_HOST', 'localhost');   
    DEFINE ('DB_NAME', 'rss'); 
    $output  = '<?xml version="1.0" encoding="UTF-8"?>';
    $output .= '<rss version="2.0">';
    $output .= '<channel>';
    $output .= '<title>Feed Demo</title>';
    $output .= '<description>News Feed from MySQL test.</description>';
    $output .= '<language>de</language>';

    $connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('-');
    mysql_select_db(DB_NAME) or die ('-');

    $query = "SELECT * FROM entry ORDER BY date DESC";
    $result = mysql_query($query) or die ('-');

    while($row = mysql_fetch_array($result)) {
        extract($row);
        $output .= '<item>';
            $output .= '<title>' . $title . '</title>';
            $output .= '<description>' . $description . '</description>';
            $output .= '<link>' . $link . '</link>';
            $output .= '<pubDate>' . date("d. F Y, H:i", strtotime($date)) . '</pubDate>';
        $output .= '</item> ';
    } 

   $output .= '</channel>';
   $output .= '</rss>';

    echo($output);
?>

Hello! 你好! I'm using the code above to generate a rss-feed from a MySql database. 我正在使用上面的代码从MySql数据库生成rss提要。 It work's very well, until I add special characters to the entries eg :,/,ä,ö,ü,ß,?," and so on. I get the message 效果很好,直到我在条目中添加特殊字符,例如:,/,ä,ö,ü,ß,?,“等。得到消息

This page contains the following errors: 此页面包含以下错误:

error on line 1 at column 377: Encoding error Everything before the row with the special characters is displayed properly. 377行1上的错误:编码错误带有特殊字符的行之前的所有内容均正确显示。

You should use utf8_encode() to encode all your values. 您应该使用utf8_encode()对所有值进行编码。

something like: 就像是:

while($row = mysql_fetch_array($result)) {
    extract($row);
    $output .= '<item>';
        $output .= '<title>' . utf8_encode($title) . '</title>';
        $output .= '<description>' . utf8_encode($description) . '</description>';
        $output .= '<link>' . $link . '</link>';
        $output .= '<pubDate>' . date("d. F Y, H:i", strtotime($date)) . '</pubDate>';
    $output .= '</item> ';
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM