【发布时间】:2018-01-11 20:09:38
【问题描述】:
我正在开发一个非常简单的 RSS 提要。我正在做的是从数据库中提取信息并使用 PHP 将其转换为 XML。但是,当我使用 Chrome 查看代码以确保其全部显示时,我在页面顶部看到了这些错误。
这是我用来从我的数据库中提取并创建 RSS Feed 的代码。
<?php
include('connectDatabaseScript.php');
$sql = "SELECT * FROM table ORDER BY id DESC";
$query = mysql_query($sql) or die(mysql_error());
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
<title>My RSS Feed</title>
<link>http://www.mywebsite.com/rss.php</link>
<description>The description for the feed.</description>
<language>en-us</language>";
while($row = mysql_fetch_array($query)) {
$title=$row['title'];
$finalTitle = str_replace("&", "and", $title);
$link=$row['link'];
$newLink = str_replace("&", "&", $link);
$category = $row['category'];
$date = $row['date'];
$description = $row['description'];
echo "<item>
<title>$finalTitle</title>
<link>$newLink</link>
<description>$description</description>
<author>John Doe</author>
<pubDate>$date<pubDate>
<category>$category</category>
</item>";
}
echo "</channel></rss>";
?>
此代码通常会卡在标题标签上。当它这样做时,它会将链接合并在一起,并且还可以合并该项目的其余部分以及它之后的其他几个项目。这是正在发生的事情的一个示例。
<item>
<title>Title No 415: Title <item>
<title>Title No 291: Another Title</title>
<link>http://www.mywebsite.com/post.php?id=291</link>
<description>description</description>
<author>John Doe</author>
<pubDate>Jan. 1, 2000</pubDate>
<category>Generic</category>
</item>
我已经弄清楚是什么角色导致了这种情况发生。是我拥有的某些标题中出现的“-”字符导致了问题。我一直在尝试使用 str_replace 函数将其删除。虽然我已经能够成功地将它与“&”一起使用,但它不能与“–”一起使用。是否有其他解决方案可以摆脱标题中的“-”,或者仍然可以使用 str_replace?
【问题讨论】: