如何清理 RSS XML 中的信息



我创建了一个RSS PHP脚本,它直接从数据库中提取信息,以便我可以在WordPress网站上提取和显示信息。我遇到的问题是XML显示一些错误

error on line xxx at column xxx: xmlParseEntityRef: no name

我尝试过使用消毒和怀孕/斯特替换,但没有运气。运行 Linux 和 MySQL 5 和 Apache 2 的 PHP 7.3.8。

<?php
// Create connection
$con = mysqli_connect("ip", "username", "password");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Database connection failed!: " . mysqli_connect_error();
}
$sql = "SELECT * FROM database.table ORDER BY id DESC LIMIT 25";
$query = mysqli_query($con,$sql);
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
<title>My Title</title>
<link>/</link>
<description>Description about the RSS Feed.</description>
<language>en-us</language>";
while($row = mysqli_fetch_array($query)) {
$rowid = $row['id'];
$id = $row['buyer_id'];
$title = $row['title'];
$message = preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $row['message']);
$message = str_replace(''', '"', $message);
$update = preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $row['update']);
$update = str_replace(''', '"', $update);
$url = $row['url'];
$created_at = $row['created_at'];
$updated_at = $row['updated_at'];
echo "<item>
<title>$title</title>
<link>$url</link>
<description>$message</description>
<update>$update</update>
<pubDate>$created_at</pubDate>
</item>";
}
echo "</channel>
</rss>";
?>

我收到此错误 https://i.imgur.com/cnQ8pjb.png 它似乎引用了一个应该被替换的 & 符号 https://i.imgur.com/PBsPjcn.png。

请问你能帮忙吗?

我自己修复了这个问题,在 HTML 部分中使用下面的方法。

<title><![CDATA[$title]]></title>
<description><![CDATA[$message]]></description>
<update><![CDATA[$update]]></update>

最新更新