0

私はこれを試しましたが、失敗しました:

$whereIsMyCake = '';

//
// put all the links here
//
$url =  "http://www.youtube.com/embed/more_yougooglelink";

$url =  "http://www.youtube.com/v/..." // works
$url =  "http://www.youtube.com/watch?v=ue1mLnyEd90"; // WORKS
$url =  "http://youtu.be/we_youGoogle_u"; // WORKS


parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
if($my_array_of_vars['v']=='') { 
  preg_match(
    "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#",
    $url, 
    $matches);

  $whereIsMyCake = $matches[0];
} else {
  $whereIsMyCake = $my_array_of_vars['v'];
}

echo $whereIsMyCake;

質問:現在youtube.com/embed/videoid is hereは機能していないようです。YouTubeが新しいリンクを作成した場合、アルゴリズムを変更し続ける必要がありますか?にとってyoutube.com/embed/embed.weareruling/embed/embed/googleisboss/findmenow/video id is here ?

4

1 に答える 1

1

この回答で対処されているように、より完全な正規表現を使用できます:https ://stackoverflow.com/a/6556662/921739 。その答えは、あなたが役に立つかもしれないYouTubeのoembedサービスについても話します。

また、YouTubeAPIhttps ://developers.google.com/youtube/2.0/developers_guide_phpを使用することもできます。そうすれば、フォーマットが変更されても、あなたはまだ黄金色です。

YouTubeのURLの変更に関する懸念を軽減するためのメモとして:YouTubeビデオへのリンクがあるすべてのブログ、Webサイト、および電子メールについて考えてみてください。古い形式をサポートせずにURL形式を変更すると、これらのリンクがすべて壊れてしまいます。Googleには、URLをめったに変更しない正当な理由があります。(それは彼らがそうしないと言っているわけではありません...)


編集

私が提供したSOリンクのコードを使用して、IDは必要なすべての形式でURLから取得されます。

function youtube_id_from_url($url) {
    $pattern = 
        '%^# Match any youtube URL
        (?:https?://)?  # Optional scheme. Either http or https
        (?:www\.)?      # Optional www subdomain
        (?:             # Group host alternatives
          youtu\.be/    # Either youtu.be,
        | youtube\.com  # or youtube.com
          (?:           # Group path alternatives
            /embed/     # Either /embed/
          | /v/         # or /v/
          | /watch\?v=  # or /watch\?v=
          )             # End path alternatives.
        )               # End host alternatives.
        ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
        $%x'
        ;
    $result = preg_match($pattern, $url, $matches);
    if (false !== $result) {
        return $matches[1];
    }
    return false;
}

$urls[] = "http://www.youtube.com/embed/ue1mLnyEd90";
$urls[] = "http://www.youtube.com/v/ue1mLnyEd90";
$urls[] = "http://www.youtube.com/watch?v=ue1mLnyEd90";
$urls[] = "http://youtu.be/ue1mLnyEd90";

foreach ($urls as $url) {
    assert(youtube_id_from_url($url) === 'ue1mLnyEd90'); # Passes
}
于 2012-10-08T12:49:18.540 に答える