2010-11-21 124 views

回答

1

使用XML Parser閱讀提要並將其放入數組中。然後,使用array_rand從數組中選取一個隨機項目。

0
<? 
function load_xml_feed($feed) 
{ 
global $RanVal; 
$i= 1; 
$FeedXml = simplexml_load_file($feed); 
foreach ($FeedXml->channel->item as $topic) { 
$title[$i] = (string)$topic->title; 
$link[$i] = (string)$topic->link; 
$description[$i] = (string)$topic->description; 
$i++; 
} 
$randtopic = rand(2, $i); 
$link = trim($link[$randtopic]); 
$title = trim($title[$randtopic]); 
$description = trim($description[$randtopic]); 
$RanVal = array($title,$link,$description); 
return $RanVal; 
} 
$rss = "http://www.sabaharabi.com/rss/rss.xml"; 
load_xml_feed($rss); 
$link = $RanVal[1]; 
$title = $RanVal[0]; 
$description = $RanVal[2]; 
echo "<h1>".$title."</h1><h2>".$link."</h2><p>".$description."</p>";