Return to Snippet

Revision: 49520
at July 23, 2011 07:15 by Gafanhoto


Initial Code
function _get_video_id( $url ) {
		if( preg_match( '/http:\/\/youtu.be/', $url, $matches) ) {
			$url = parse_url($url, PHP_URL_PATH);
			$url = str_replace( '/', '',  $url);
			return $url;
	
		} elseif ( preg_match( '/watch/', $url, $matches) ) {
			$arr = parse_url($url);
			$url = str_replace( 'v=', '', $arr['query'] );
			return $url;
		
		} elseif ( preg_match( '/http:\/\/www.youtube.com\/v/', $url, $matches) ) {
			$arr = parse_url($url);
			$url = str_replace( '/v/', '', $arr['path'] );
			return $url;
	
		} elseif ( preg_match( '/http:\/\/www.youtube.com\/embed/', $url, $matches) ) {
			$arr = parse_url($url);
			$url = str_replace( '/embed/', '', $arr['path'] );
			return $url;
			
		} elseif ( preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches) ) {
			return $matches[0];
			
		} else {
			return false;
		}
	}

Initial URL


Initial Description
Return the YouTube video ID.

Initial Title
Get YouTube Video ID

Initial Tags


Initial Language
PHP