RSS (Rich Site Summary), also known as Really Simple
Syndication is used to publish frequently updated information in standard
format. It an easy way to make the content available to various platforms. It is
therefore useful to have RSS feeds for our website. In this tutorial, we will learn
how to generate RSS feeds of our website’s content through PHP.
The following is the is the RSS feed structure:
<?xml version="1.0"
encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title></title>
<link></link>
<description></description>
<language></language>
<item>
<title></title>
<link></link>
<description></description>
</item>
</channel>
</rss>
Now we will write a PHP program to extract data from the
MySQL database and generate RSS feed.
<?php
//Function to remove unwanted characters from the string.
function clean_input($input) {
$search = array(
'@<script[^>]*?>.*?</script>@si',
/* Remove javascript */
'@<[\/\!]*?[^<>]*?>@si', /*
Remove HTML tags */
'@<style[^>]*?>.*?</style>@siU', /*
Remove style tags */
'@<![\s\S]*?--[
\t\n\r]*>@' /* Remove
multi-line comments */
);
$output = preg_replace($search, '',
$input);
return $output;
}
?>
<?xml version="1.0"
encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>WEBSITE_TITLE</title>
<link>WEBSITE_LINK</link>
<description>DESCRIPTION_TEXT</description>
<language>LANGUAGE_USED</language>
<?php
$query
= 'SELECT * FROM `content` ORDER BY `id` DESC';
$rs
= mysql_query($query);
while($row = mysql_fetch_assoc($rs)) {
$post_title
= clean_input($row['title']);
$url
= clean_input($row['url']);
$description = clean_input($row['short_description']);
#
Putting in the blog posts information for the user.
echo
'<item>';
echo
'<title>'.$post_title.'</title>';
echo
'<link>'.$url.'</link>';
echo
'<description><![CDATA['.$description.']]></description>';
echo
'</item>';
}
}
?>
</channel>
</rss>
The above code gets the content from the MySQL database and
displays it in RSS format.
No comments :
Post a Comment