douxuzui4590 2011-03-05 09:50
浏览 21
已采纳

这个php RSS提要有什么问题?

This is the code I am using:

<?php
include "global.php";

$query = "SELECT * FROM posts WHERE `approved`='1' ORDER BY time DESC";
$res = mysql_query($query) or die(mysql_error());

$xml_output = "<?xml version=\"1.0\"?>
";
$xml_output .= "<rss version=\"2.0\">

";
$xml_output .= "<channel>

";

while($row = mysql_fetch_assoc($res)){
    $id = $row['id'];
    $title = $row['title'];
    $content = ShortenText($row['content'], 500);

    $xml_output .= "\t<item>
";
    $xml_output .= "\t\t<title>" . $title . "</title>
";
        $content = str_replace("&", "&", $content);
        $content = str_replace("<", "<", $content);
        $content = str_replace(">", "&gt;", $content);
        $content = str_replace("\"", "&quot;", $content);
    $xml_output .= "\t\t<description>" . $content . "</description>
";
    $xml_output .= "\t\t<link>" . "http://projectstratos.com/post.php?id=" . $row['id'] . "</link>
";
    $xml_output .= "\t</item>
";
}

$xml_output .= "</channel>

"; 
$xml_output .= "</rss>";

echo $xml_output;
?>

And it shows a blank rss feed page, why is this?

展开全部

  • 写回答

2条回答 默认 最新

  • douwen1213 2011-03-05 09:54
    关注

    The problem is because you aren't correctly escaping the XML here:

    $content = str_replace("&", "&", $content);
    $content = str_replace("<", "<", $content);
    

    These two lines aren't actually doing anything. You are just replacing a string with the same string. Did you mean &amp; instead of & and &lt; instead of < for the replacement strings?

    A better way is to use CDATA tags but this still won't escape all characters correctly.

    Even better still - use the DOM classes to generate the XML for you rather than escaping it yourself.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部