これをオンラインでhttp://snipplr.com/view/62238/get-youtube-video-id-very-robust/で見つけました
function getYouTubeId($url) {
// Format all domains to http://domain for easier URL parsing
str_replace('https://', 'http://', $url);
if (!stristr($url, 'http://') && (strlen($url) != 11)) {
$url = 'http://' . $url;
}
$url = str_replace('http://www.', 'http://', $url);
if (strlen($url) == 11) {
$code = $url;
} else if (preg_match('/http:\/\/youtu.be/', $url)) {
$url = parse_url($url, PHP_URL_PATH);
$code = substr($url, 1, 11);
} else if (preg_match('/watch/', $url)) {
$arr = parse_url($url);
parse_str($url);
$code = isset($v) ? substr($v, 0, 11) : false;
} else if (preg_match('/http:\/\/youtube.com\/v/', $url)) {
$url = parse_url($url, PHP_URL_PATH);
$code = substr($url, 3, 11);
} else if (preg_match('/http:\/\/youtube.com\/embed/', $url, $matches)) {
$url = parse_url($url, PHP_URL_PATH);
$code = substr($url, 7, 11);
} else if (preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches) ) {
$code = substr($matches[0], 0, 11);
} else {
$code = false;
}
if ($code && (strlen($code) < 11)) {
$code = false;
}
return $code;
}