【问题标题】:Make slug for RSS Feed为 RSS Feed 制作 slug
【发布时间】:2013-02-27 16:22:40
【问题描述】:

我想创建指向 RSS 帖子的链接,但我需要一个“slug”。我想用“-”替换空格,这样我就可以将链接重定向到我的 wordpress 网站上的帖子。我的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>";

我做了一些研究,知道你可以这样做:$title = preg_replace("/[\s-]+/", "-", $title);

所以我想用'-'而不是空格来创建一个标题。

但我不知道如何在我的代码中实现这一点。

提前致谢!

【问题讨论】:

    标签: php rss slug


    【解决方案1】:

    只需使用 str_replace 函数。 http://php.net/manual/en/function.str-replace.php

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

    【讨论】:

    【解决方案2】:

    我将首先将 mysql 函数更改为 mysqli 函数,因为不推荐使用 mysql!

    【讨论】:

      猜你喜欢
      • 2012-03-19
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      相关资源
      最近更新 更多