Return to Snippet

Revision: 54558
at May 10, 2012 04:32 by jmiller


Updated Code
/**
 * Get 11-character YouTube video ID from a URL
 *
 * @param string $url URL to video
 * @return string|false False on invalid URL, 11 character string on success
 */
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;
}

Revision: 54557
at January 3, 2012 17:17 by jmiller


Initial Code
/**
 * Get 11-character YouTube video ID from a URL
 *
 * @param string $url URL to video
 * @return string|false False on invalid URL, 11 character string on success
 */
function getYouTubeId($url) {
	// Format all domains to http://domain for easier URL parsing
	str_replace('https://', 'http://', $url);
	if (!stristr($url, 'http://')) {
		$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;
}

Initial URL


Initial Description
This will get the 11-character YouTube video ID from any valid YouTube URL.

Based on http://snipplr.com/view/57065/get-youtube-video-id/ but made more robust to handle additional URL inputs

Initial Title
Get YouTube Video ID [Very robust]

Initial Tags
regex, url, video, code

Initial Language
PHP