从youtube api输出中删除videoid

时间:2014-01-19 19:31:59

标签: php youtube-api

我正在使用youtube api搜索视频。我的问题是,当搜索完成时,搜索的输出是

video_name  video_id

我只是想让它显示

video_name

继承我的代码以显示搜索结果

 $client = new Google_Client();
$client->setDeveloperKey($DEVELOPER_KEY);


$youtube = new Google_Service_YouTube($client);

try {

$searchResponse = $youtube->search->listSearch('id,snippet', array(
  'q' => $_GET['q'],
  'maxResults' => $_GET['maxResults'],
));

$videos = '';
$channels = '';
$playlists = '';

// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach ($searchResponse['items'] as $searchResult) {
 $imgsrc="<img src=http://img.youtube.com/vi/".$searchResult['id']
               ['videoId']."/hqdefault.jpg height=125 width=125>";
  switch ($searchResult['id']['kind']) {  
    case 'youtube#video':  
      $videos .= sprintf('<li>'.$imgsrc.' %s (%s)</li>', $searchResult['snippet']['title'],  
        $searchResult['id']['videoId']."<a href=/video.php?".$searchResult['id']['videoId']." target=_blank>   Watch This Video</a>");  
      break;  

1 个答案:

答案 0 :(得分:1)

我对sprintf不太熟悉,但你想删除

$searchResult['id']['videoId']

不破坏您的代码。

我建议将其包装在特定字符串之间,例如

vidid[".$searchResult['id']['videoId']."]

然后删除vidid[]之间的所有内容。