2

我有一个 RSS 源:

http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY
<item>
      <title>2008. november 23.</title>
      <link>http://www.mr1-kossuth.hu/m3u/0039c36f_3003051.m3u</link>
      <description>........</description>
      <pubDate>Wed, 26 Nov 2008 00:00:00 GMT</pubDate>
    </item>

由此,我想创建一个对播客友好的提要。我想将 LINK 孩子替换为:

http://stream001.radio.hu:8000/content/*.mp3

例子:

<item>
      <title>2008. november 23.</title>
      <link>http://stream001.radio.hu:8000/content/0039c36f_3003051.mp3</link>
      <description>........</description>
      <pubDate>Wed, 26 Nov 2008 00:00:00 GMT</pubDate>
    </item>

我如何在 PHP 中做到这一点?

4

4 回答 4

3

这是一个简单而纯粹的 XSLT 1.0 解决方案,仅包含 47 行,其中一半是结束标记。以下转换

<xsl:stylesheet 版本="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:输出省略-xml-declaration="yes"/>

 <xsl:param name="pNewLink"
 select="'http://stream001.radio.hu:8000/content/'"/>

 <xsl:param name="pNewExt" select="'.mp3'"/>

    <xsl:template match="node()|@*">
      <xsl:复制>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:模板>

    <xsl:template match="链接">
      <xsl:复制>
         <xsl:copy-of select="@*"/>

         <xsl:变量名="vFName">
           <xsl:call-template name="GetFileName">
             <xsl:with-param name="pFPath" select="."/>
           </xsl:调用模板>
         </xsl:变量>

         <xsl:value-of select="concat($pNewLink,$vFName,$pNewExt)"/>
      </xsl:copy>
    </xsl:模板>

    <xsl:template name="GetFileName">
      <xsl:param name="pFPath"/>

      <xsl:选择>
        <xsl:when test="not(contains($pFPath, '/'))">
          <xsl:value-of select="substring-before($pFPath, '.')"/>
        </xsl:when>

        <xsl:否则>
          <xsl:call-template name="GetFileName">
            <xsl:with-param name="pFPath"
             select="substring-after($pFPath, '/')"/>
          </xsl:调用模板>
        </xsl:否则>
      </xsl:选择>
    </xsl:模板>
</xsl:样式表>

当应用于提供的源 XML 文档时

<项目>
    <标题>2008. 11 月 23 日。</title>
    <link>http://www.mr1-kossuth.hu/m3u/0039c36f_3003051.m3u</link>
    <description>........</description>
    <pubDate>格林威治标准时间 2008 年 11 月 26 日星期三 00:00:00</pubDate>
</项目>

产生想要的结果

<项目>
    <标题>2008. 11 月 23 日。</title>
    <link>http://stream001.radio.hu:8000/content/0039c36f_3003051.mp3</link>
    <description>........</description>
    <pubDate>格林威治标准时间 2008 年 11 月 26 日星期三 00:00:00</pubDate>
</项目>

请注意此解决方案的以下特定功能:

  1. 我们使用 XSLT 设计模式来使用和覆盖身份转换

  2. 名为“GetFileName ”的模板从完整的 URL(作为参数传递)中提取,只是去掉了文件扩展名的文件名。这是递归调用自身的命名模板的一个很好的例子

  3. 所需新 URL 的组成部分被指定为 global <xsl:param>s
于 2008-11-27T00:21:04.800 回答
1

这是最好用XSLT完成的事情。您可以编写一个简单的 XSL 转换来执行此操作,或者您可以使用 hacky 方式并使用 preg_match、preg_replace 和朋友。

于 2008-11-26T19:22:34.557 回答
0

最简单的方法是简单的字符串替换:

$str = file_get_contents('http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY');
$str = str_replace('<link>http://www.mr1-kossuth.hu/m3u/', '<link>http://stream001.radio.hu:8000/content/', $str);
$str = str_replace('m3u</link>', 'mp3</link>', $str);

完毕!

于 2008-11-26T19:24:47.847 回答
0
<?php
 $str = file_get_contents('http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY');
 $xml = simplexml_load_string($str);
if ($xml) {

$intro = str_replace('<link>http://www.mr1-kossuth.hu/m3u/', '<enclosure url="http://stream001.radio.hu:8000/content/', $xml->asXML());
$intro = str_replace('m3u</link>', 'mp3" type="audio/mpeg" />', $intro);
echo $intro;    
} else {
    $error = "Could not load intro XML file.";
}

?>
于 2008-11-26T20:59:51.150 回答