douxuzui4590 2011-03-05 17: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 17: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条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊