I have an RSS source:
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>
From this, I want to create a podcast-friendly feed. I want to replace the LINK children to:
http://stream001.radio.hu:8000/content/*.mp3
Example:
<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>
How I can do that in PHP?
This is a simple and pure XSLT 1.0 solution , consisting of just 47 lines, half of which are closing tags. The following transformation :
<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>
when applied on the provided source XML document :
<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>
produces the wanted result :
<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>
Do note the following specific features of this solution:
We use the XSLT design pattern of using and overriding the identity transformation .
The template named "GetFileName " extracts from the complete URL (passed as parameter), just the filename with the file extension stripped off. This is a good example of a named template that calls itself recursively .
<xsl:param>
s That's the sort of thing that's best done with an XSLT . You could write a simple XSL transform to do it, or you could do the hacky way and use the preg_match, preg_replace, and friends.
The easiest way is a simple string replace:
$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);
Done!
<?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.";
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.