如何将变量Feed转换为数组?

时间:2013-08-28 16:32:25

标签: php rss

我想将变量转换为数组,以便存储多个Feed?

<?php 
error_reporting(0);

$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');

$xml = $feed_lifehacker_full;
//print_r($xml); 

foreach ($xml->channel->item as $node){

$title = $node->title; 
$link = $node->link; 
$link = explode('/', $link);
$link = $link[8];
$url = $node->url;
$description = $node->description; 
$pubDate = $node->pubDate;
preg_match_all('#(http://img[^\s]+(?=\.(jpe?g|png|gif)))#i', $description[0], $images);
$images = $images[0][1] . '.jpg';
if($images == '.jpg'){

    //uncomment to show youtube articles
    //$images = "http://placehold.it/640x360";
    //echo  "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
} else {
    //article image
    $images . '<br>';
    echo  "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
}


}

如何更改此内容以加载到数组

$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');

$xml = $feed_lifehacker_full;

该脚本只是收集rss Feed的图像并链接到页面,如果您看到如何更高效地完成它,请随意说

1 个答案:

答案 0 :(得分:0)

可以将给定的结果编码为json,并通过解码它将返回一个数组

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json, TRUE);