如何清理 RSS 中的信息 XML

How to sanitse information in RSS XML

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

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

我试过使用消毒和 preg/str 替换但没有成功。 PHP 7.3.8 运行 Linux 和 MySQL 5 和 Apache 2.

<?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 which seems to be referencing an & sign which should be getting replaced. https://i.imgur.com/PBsPjcn.png

请问你能帮忙吗?

我自己解决了这个问题,使用下面的 HTML 部分。

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