Php 如何替换链接子项以创建播客友好的提要?
我有一个RSS源:Php 如何替换链接子项以创建播客友好的提要?,php,xml,xslt,rss,Php,Xml,Xslt,Rss,我有一个RSS源: http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY 例如: <item> <title>2008. november 23.</title> <link>http://stream001.radio.hu:8000/content/0039c36f_3003051.mp3</link> <description>...
http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY
例如:
<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>
2008十一月二十三号。
http://stream001.radio.hu:8000/content/0039c36f_3003051.mp3
........
2008年11月26日星期三00:00:00 GMT
我如何在PHP中做到这一点?这是最好用一个。您可以编写一个简单的XSL转换来实现这一点,也可以使用hacky方法并使用preg_match、preg_replace和friends 最简单的方法是简单的字符串替换:
$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);
$str=file\u get\u contents('http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY');
$str=str_替换('http://www.mr1-kossuth.hu/m3u/', 'http://stream001.radio.hu:8000/content/“,$str);
$str=str_替换('m3u','mp3',$str);
完成了 asXML());
$intro=str_replace('m3u','mp3“type=“audio/mpeg”/>,$intro);
echo$intro;
}否则{
$error=“无法加载简介XML文件。”;
}
?>
这是一个简单而纯粹的XSLT 1.0解决方案,仅由47行组成,其中一半是结束标记。以下转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-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:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="vFName">
<xsl:call-template name="GetFileName">
<xsl:with-param name="pFPath" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat($pNewLink,$vFName,$pNewExt)"/>
</xsl:copy>
</xsl:template>
<xsl:template name="GetFileName">
<xsl:param name="pFPath"/>
<xsl:choose>
<xsl:when test="not(contains($pFPath, '/'))">
<xsl:value-of select="substring-before($pFPath, '.')"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="GetFileName">
<xsl:with-param name="pFPath"
select="substring-after($pFPath, '/')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<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>
<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>
应用于提供的源XML文档时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-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:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="vFName">
<xsl:call-template name="GetFileName">
<xsl:with-param name="pFPath" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat($pNewLink,$vFName,$pNewExt)"/>
</xsl:copy>
</xsl:template>
<xsl:template name="GetFileName">
<xsl:param name="pFPath"/>
<xsl:choose>
<xsl:when test="not(contains($pFPath, '/'))">
<xsl:value-of select="substring-before($pFPath, '.')"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="GetFileName">
<xsl:with-param name="pFPath"
select="substring-after($pFPath, '/')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<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>
<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>
2008年11月23日。
http://www.mr1-kossuth.hu/m3u/0039c36f_3003051.m3u
........
2008年11月26日星期三00:00:00 GMT
产生想要的结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-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:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="vFName">
<xsl:call-template name="GetFileName">
<xsl:with-param name="pFPath" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat($pNewLink,$vFName,$pNewExt)"/>
</xsl:copy>
</xsl:template>
<xsl:template name="GetFileName">
<xsl:param name="pFPath"/>
<xsl:choose>
<xsl:when test="not(contains($pFPath, '/'))">
<xsl:value-of select="substring-before($pFPath, '.')"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="GetFileName">
<xsl:with-param name="pFPath"
select="substring-after($pFPath, '/')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<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>
<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>
2008年11月23日。
http://stream001.radio.hu:8000/content/0039c36f_3003051.mp3
........
2008年11月26日星期三00:00:00 GMT
请注意此解决方案的以下特定功能:
s