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:



$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=“”>

Nieuwsfeed WebGiants Financieel
Nieuwsfeed WebGiants

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>".$item->title."</link>";
	echo '</item>'; 


echo “”;
echo “”;[/php]

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

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

Thanks in advance!

That appears to do the opposite of what you want. That code would replace spaces (\s) and dashes (-) with a single space. It could be used to replace multiple spaces or multiple dashes with a single space.

Thanks for the reply. I see, so I need something like this: $title = preg_replace("/[\s]+/", “-”, $title); ?

And how can I implement this code in my PHP file? Simply add this line isnt working.

Yes, exactly. Your code should work the problem is that you are echoing the object ($item->title) instead of the variable ($title). Also you specified $title in the preg_replace but it should be the object, for example:

[php]$title = preg_replace("/\s+/", “-”, $item->title)[/php]

Then your echo should use $title

[php]echo “”. $title ."";[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service