tags:

views:

156

answers:

5

Hi,

i have a 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&lt;/link&gt;
      <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 childs, 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&lt;/link&gt;
      <description>........</description>
      <pubDate>Wed, 26 Nov 2008 00:00:00 GMT</pubDate>
    </item>

How I can do that in php?

+1  A: 

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.

Benson
A: 

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!

Greg
A: 

But with this method i cant change the description, release date etc. This is only possible with using the SimpleXML? How?

+1 question, it is possible to determine the file size of a streamed audio?
With SimpleXML in PHP, you can download the file and get its size.
che
A: 

http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY'); $xml = simplexml_load_string($str); if ($xml) {

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

?>

+3  A: 

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&lt;/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&lt;/link>
    <description>........</description>
    <pubDate>Wed, 26 Nov 2008 00:00:00 GMT</pubDate>
</item>

Do note the following specific features of this solution:

  1. We use the XSLT design pattern of using and overriding the identity transformation.

  2. 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.

  3. The constituents of the desired new URLs are specified as global <xsl:param>s
Dimitre Novatchev