0

I am creating a podcast "widget" for a client/friend who hosts a science show.

This code pulls the title and mp3 podcast from an xml feed into jplayer http://www.freeenergymedia.com/shared/PLAYER/player/player.php

reset($titles[1])

is pulling the very last element in the feed while

end($out[1])

is pulling the most recent

I need them to match and both display the most recent

$c = file_get_contents('http://rss.sonibyte.com/rssfeed/56.xml');
preg_match_all('/enclosure url="(.*)" length/', $c, $out, PREG_PATTERN_ORDER);
preg_match_all("|<[title]+>StarTalk:(.*)</[title]+>|U", $c, $titles, PREG_PATTERN_ORDER);
if(sizeof($out[1]) >= 3 && sizeof($titles[1]) >= 3) {
    echo '[';   
    $e = end($out[1]); $b = reset($titles[1]);   echo '{ title:"StarTalk:' . $b . '", mp3:"'.$e.'" },';
    $e = prev($out[1]); $b = next($titles[1]);  echo '{ title:"StarTalk:' . $b . '", mp3:"'.$e.'" },';
    $e = prev($out[1]); $b = next($titles[1]);  echo '{ title:"StarTalk:' . $b . '", mp3:"'.$e.'" }';
    echo ']';
}

the php is inserted into the JPlayer plugin here, to display the mp3 url and title

$(document).ready(function(){

    new jPlayerPlaylist({
        jPlayer: "#jquery_jplayer_1",
        cssSelectorAncestor: "#jp_container_1"
    }, <?php include('getxmlforplayer.php') ?>, {
        swfPath: "js",
        supplied: "mp3, oga",
        wmode: "window"
    });
});
Alex Borsody
  • 1,908
  • 9
  • 40
  • 74
  • 2
    [Don't parse XML with regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)! – Francis Avila Nov 30 '11 at 18:24
  • 2
    Agreed, try SimpleXML, this is 2011 :). We have easier tools now friend. – Darren Nov 30 '11 at 18:27
  • Thanks, I will check it out, it seemed to work for the title though. – Alex Borsody Nov 30 '11 at 18:32
  • 2
    Here is a link to get you started with SimpleXML and RSS. http://blog.stuartherbert.com/php/2007/01/07/using-simplexml-to-parse-rss-feeds/ – Tomalak Nov 30 '11 at 18:50

2 Answers2

4

"getting the first XML element in a feed" would be like this:

$rss = simplexml_load_string('http://rss.sonibyte.com/rssfeed/56.xml');

$item = $rss->channel->item[0];

print_r($item);

Check out how SimpleXML works in the docs. Its actually pretty easy. Don't ever parse XML with regular expressions.

Getting the link would be as simple as

$url = (string)$item->link
Tomalak
  • 332,285
  • 67
  • 532
  • 628
4

Never parse XML with regular expressions.

Use SimpleXML or DOMDocument instead. Below reimplements all your code with SimpleXML.

$url = 'http://rss.sonibyte.com/rssfeed/56.xml';
$rss = simplexml_load_file($url);

$items = $rss->channel->item;

// first item is this:
$items[0];
// first title:
$items[0]->title;
// first url:
$items[0]->enclosure['url'];

$data = array();
foreach ($items as $item) {
    $data[] = array(
        'title' => (string) $item->title,
        'mp3'   => (string) $item->enclosure['url'],
    );
}

$jsdata = json_encode($data);

Your javascript:

$(document).ready(function(){
    var playerdata = <?php echo htmlspecialchars($jsdata, ENT_NOQUOTES, 'utf-8');?>;

    new jPlayerPlaylist({
        jPlayer: "#jquery_jplayer_1",
        cssSelectorAncestor: "#jp_container_1"
    }, playerdata, {
        swfPath: "js",
        supplied: "mp3, oga",
        wmode: "window"
    });
});
Francis Avila
  • 31,233
  • 6
  • 58
  • 96