2011-08-28 167 views
-1

我試圖創建一個函數,它調用一個來自Wordpress自定義字段的值(「_videourl」爲YouTube視頻URL),然後使用PHP剪裁將其剪裁爲YouTube視頻ID。我發現,減少了網址只是ID的JavaScript函數,但我不知道我怎麼會能夠翻譯成PHP(以下功能):從URL獲取YouTube視頻ID w/PHP

 function youtubeIDextract(url) 
    { 
    var youtube_id; 
    youtube_id = url.replace(/^[^v]+v.(.{11}).*/,"$1"); 
    return youtube_id; 
    } 

這個PHP函數將在循環內部使用所以我認爲我將不得不使用變量,但我真的只是一個小白,所以我不知道該怎麼做。任何人都可以通過分享他們的編碼專業知識來幫助我創建PHP函數嗎?

編輯:解決

一些實驗後,我找到了解決辦法。我想返回併發布它,以便其他需要的人也可以從某個地方開始。

function getYoutubeId($ytURL) 
    { 
     $urlData = parse_url($ytURL); 
     //echo '<br>'.$urlData["host"].'<br>'; 
     if($urlData["host"] == 'www.youtube.com') // Check for valid youtube url 
     { 
      $ytvIDlen = 11; // This is the length of YouTube's video IDs 

      // The ID string starts after "v=", which is usually right after 
      // "youtube.com/watch?" in the URL 
      $idStarts = strpos($ytURL, "?v="); 

      // In case the "v=" is NOT right after the "?" (not likely, but I like to keep my 
      // bases covered), it will be after an "&": 
      if($idStarts === FALSE) 
       $idStarts = strpos($ytURL, "&v="); 
      // If still FALSE, URL doesn't have a vid ID 
      if($idStarts === FALSE) 
       die("YouTube video ID not found. Please double-check your URL."); 

      // Offset the start location to match the beginning of the ID string 
      $idStarts +=3; 

      // Get the ID string and return it 
      $ytvID = substr($ytURL, $idStarts, $ytvIDlen); 

      return $ytvID; 
     } 
     else 
     { 
      //echo 'This is not a valid youtube video url. Please, give a valid url...'; 
      return 0; 
     } 

    } 
+3

這是否解決您的問題:HTTP:// stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match/6382259#6382259? –

+0

我很懷疑YouTube的視頻ID總是11個字符。你的正則表達式可能是錯誤的。 – MartinodF

+0

我在鏈接Peter中發現了一些有用的提示,但似乎沒有任何解決方案能夠在最後過濾出額外的參數(這也是我所需要的)。我正在做一些研究,並發現這一點:http://www.halgatewood.com/php-get-the-youtube-video-id-from-a-youtube-url/,但我不知道如何改變它獲取字段'_videoembed'的自定義字段值 – Matt

回答

0

假設正則表達式是正確的,你可以使用preg_replace

$youtubeId = preg_replace('/^[^v]+v.(.{11}).*/', '$1', $url); 

您還可能有興趣在str_replacesubstr作爲替代品。

12

我不得不處理這個問題,因爲我幾個星期前寫了一個PHP類,最後是一個匹配任何類型字符串的正則表達式:帶或不帶URL的方案,帶或不帶子域,youtube.com URL字符串, youtu.be URL字符串並處理所有類型的參數排序。你可以檢查出來at GitHub或簡單地複製和粘貼下面的代碼塊:

/** 
* Check if input string is a valid YouTube URL 
* and try to extract the YouTube Video ID from it. 
* @author Stephan Schmitz <[email protected]> 
* @param $url string The string that shall be checked. 
* @return mixed   Returns YouTube Video ID, or (boolean) false. 
*/   
function parse_yturl($url) 
{ 
    $pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x'; 
    preg_match($pattern, $url, $matches); 
    return (isset($matches[1])) ? $matches[1] : false; 
} 

要解釋的正則表達式,這裏有一個溢出的後續版本:

/** 
* Check if input string is a valid YouTube URL 
* and try to extract the YouTube Video ID from it. 
* @author Stephan Schmitz <[email protected]> 
* @param $url string The string that shall be checked. 
* @return mixed   Returns YouTube Video ID, or (boolean) false. 
*/   
function parse_yturl($url) 
{ 
    $pattern = '#^(?:https?://)?'; # Optional URL scheme. Either http or https. 
    $pattern .= '(?:www\.)?';   # Optional www subdomain. 
    $pattern .= '(?:';    # Group host alternatives: 
    $pattern .= 'youtu\.be/';  # Either youtu.be, 
    $pattern .= '|youtube\.com'; # or youtube.com 
    $pattern .= '(?:';    # Group path alternatives: 
    $pattern .=  '/embed/';  #  Either /embed/, 
    $pattern .=  '|/v/';   #  or /v/, 
    $pattern .=  '|/watch\?v='; #  or /watch?v=,  
    $pattern .=  '|/watch\?.+&v='; #  or /watch?other_param&v= 
    $pattern .= ')';    # End path alternatives. 
    $pattern .= ')';     # End host alternatives. 
    $pattern .= '([\w-]{11})';  # 11 characters (Length of Youtube video ids). 
    $pattern .= '(?:.+)?$#x';   # Optional other ending URL parameters. 
    preg_match($pattern, $url, $matches); 
    return (isset($matches[1])) ? $matches[1] : false; 
} 
+0

我可以證實這對我有效。優秀的解決方案和解釋 –

+0

非常好,謝謝你的解釋。 – Sarah