Search code examples
phprssslug

Make slug for RSS Feed


I want to create links to RSS posts, but I need a 'slug'. I want to replace the whitespaces with a "-", so I can redirect the links to my posts on my wordpress site. My RSS:

<?php

include("connect.inc.php");

$items = mysql_query("select * from dailynewsitems where actief = '1' and pubDate <=      NOW() order by id DESC limit 15");

echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>
<rss version=\"2.0\"  xmlns:atom=\"http://www.w3.org/2005/Atom\">
<channel>
<title>Nieuwsfeed WebGiants Financieel</title>
<description>Nieuwsfeed WebGiants</description>
<link>http://modules.publiceer.net/nieuwsfeed_webgiants2012.php</link>
<language>nl</language>";

while ($item = mysql_fetch_object($items)) {
    $id = $item->id;
    $title = $item->title;
    $description = $item->description;
    $longdesc = $item->longdesc;

    list($d, $m, $y) = explode("-", $item->pubDate); 

    echo '<item>'; 
    echo '<guid>' . $item->id . '</guid>';
    echo '<title>' . htmlspecialchars($title) . '</title>';
    echo 'DATUM<pubDate>' . date("D", mktime(0, 0, 0, $m, $d, $y)) . ', ' . $d . ' ' . date("M", strtotime($item->pubDate)) . ' ' . $y . ' 00:00:00 CST</pubDate>';
    echo '<description>' . htmlspecialchars($description) . '</description>';
    echo '<longdesc>' . htmlspecialchars($longdesc) . '</longdesc>';
    echo "<link>http://www.webgiants.nl/geen-categorie/".$item->title."</link>";
    echo '</item>'; 
} 


echo "</channel>";
echo "</rss>";

I did some research and know you can do it with something like this: $title = preg_replace("/[\s-]+/", "-", $title);

So I want to create a title with '-' in stead of whitespaces.

But I dont know how to implement this in my code.

Thanks in advance!


Solution

  • Just use str_replace function. http://php.net/manual/en/function.str-replace.php

    echo "<link>http://www.webgiants.nl/geen-categorie/".str_replace(" ", "-", $item->title)."</link>";