Php代码工作得不如预期的好

Php代码工作得不如预期的好,php,api,youtube,str-replace,Php,Api,Youtube,Str Replace,这个代码有什么问题 $feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/US/most_viewed?v=2&time=all_time'; $site_url = 'http://localhost/'; // read feed into SimpleXML object $sxml = simplexml_load_file($feedURL); // iterate over entries in fe

这个代码有什么问题

$feedURL = 'http://gdata.youtube.com/feeds/api/standardfeeds/US/most_viewed?v=2&time=all_time';
$site_url = 'http://localhost/';    
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];  
$video_id = str_replace('http://www.youtube.com/watch?v=', '',$watch);
$video_id = str_replace('&feature=youtube_gdata', '',$video_id);
$video_id = str_replace('_player', '',$video_id);
echo $details .= '
'.$site_url.'video/'.$video_id.'';}
代码应返回25个url的列表(通过youtube api提要),格式如下:

但是代码返回一个丑陋的url列表(我想要的格式)混合和重复

PS:str_replace之前,链接显示正确,但格式如下:


怎么了?如何解决此问题?

echo
$details.='..'$details之前未定义,因此请使用
echo$details='…'

最后一行是问题所在:您将每个条目添加到$details中,并对其进行额外的回显。放

echo $details;

在你的回路后面,去掉回路中的回声,就这样。使用当前代码,您可以通过将其添加到变量中并回显来重复内容。

您是否可以发布收到的URL列表和收到的“丑陋”URL列表?谢谢。我没看到。看来这就是问题所在。