RSS Feed with Missing Tags

January 12, 2018, at 04:47 AM

I'm working on a very simple RSS Feed. What I am doing is pulling the information from a database and transforming it into XML using PHP. However, when I use Chrome to look at the code to make sure it is all appearing as it should, I get these errors at the top of the page.

Here is the code that I am using to pull from my database and create the RSS Feed.

$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'>
<title>My RSS Feed</title>
<description>The description for the feed.</description>
while($row = mysql_fetch_array($query)) {
$finalTitle = str_replace("&", "and", $title);
$newLink = str_replace("&", "&amp;", $link);
$category = $row['category'];
$date = $row['date'];
$description = $row['description'];
echo "<item> 
<author>John Doe</author>
echo "</channel></rss>"; 

This code usually gets stuck on the title tag. When it does that, it will merge together the link and can also merge the rest of the item and several others after it. Here is an example of what is happening.

<title>Title No 415: Title <item> 
<title>Title No 291: Another Title</title>
<author>John Doe</author>
<pubDate>Jan. 1, 2000</pubDate>

I have figured out what character is causing this to occur. It is the "–" character that appears in some of the titles that I have that is causing the problem. I've been trying to remove it by using the str_replace function. While I have been able to use it with "&" with success, it is not working with "–". Is there another solution to get rid of the "–" from the title or is it still possible with str_replace?

Answer 1

You should not write your XML like this. To avoid this kind of errors, you may use DOMDocument to write your XML, and save it using saveXML.

Answer 2

I have some PHP scripts that make a MySQL query and use it to produce an RSS feed. The text for RSS elements such as title and description needs to be cleaned up for presentation as XML.

Here's a function to do that:

function clean_text($in_text) {
    return utf8_encode(

I think a simpler function might solve the problem you're having:

function clean_text($in_text) {
    return htmlspecialchars(

The call to utf8_encode() encodes an ISO-8859-1 string as UTF-8 and was necessary for me because I was dealing with ISO-8859-1 character encoding in my database. The htmlspecialchars() function in PHP turns & to &amp;, < to &lt; and > to &gt;.

Here's a statement that uses the function to output some RSS:

echo "<description>" . clean_text($row['description']) . "</description>";
Rent Charter Buses Company
How can i get the value of a field in sql and give it to a php variable?

How can i get the value of a field in sql and give it to a php variable?

My problem is i have to update the value of the field 'alias' on the table usuarisBut i don't know how to get this value and give it to the variable i want so i can update the row later

PHP 7 post_max_size ini setting work for limiting raw POST data?

PHP 7 post_max_size ini setting work for limiting raw POST data?

I am using Apache Thrift to move small base64 encoded files to a PHP backend (with Apache web server)It is essentially just an HTTP POST request with large amounts of raw body data

Output from Form with PHP IF statment [duplicate]

Output from Form with PHP IF statment [duplicate]

This question already has an answer here:

php MariaDb Got packets out of order

php MariaDb Got packets out of order

I trying to login myphpadmin by public_IP:3307 but it always show me