【问题标题】:How to sanitse information in RSS XML [duplicate]如何清理 RSS XML 中的信息 [重复]
【发布时间】:2019-10-01 03:30:14
【问题描述】:

我创建了一个 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,它似乎引用了一个应该被替换的 & 符号。 https://i.imgur.com/PBsPjcn.png

你能帮忙吗?

【问题讨论】:

  • 如果您将 XML 生成为文本,请使用 htmlspecialchars()。或者使用 XMLWriter 或 DOM 等 XML API。

标签: php xml rss


【解决方案1】:

自己修复了这个问题,使用 HTML 部分中的以下内容。

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

【讨论】:

    猜你喜欢
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多