Created
July 17, 2014 22:38
-
-
Save howardgrigg/d4d7abeb5aea4eb8e048 to your computer and use it in GitHub Desktop.
Get Youtube ID from URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function YouTubeID(){ | |
if($youtube_url = $this->YouTubeLink){ | |
$url = parse_url($youtube_url); | |
if( $url['host'] !== 'youtube.com' && | |
$url['host'] !== 'www.youtube.com'&& | |
$url['host'] !== 'youtu.be'&& | |
$url['host'] !== 'www.youtu.be') | |
return ''; | |
if( $url['host'] === 'youtube.com' || $url['host'] === 'www.youtube.com' ) : | |
parse_str(parse_url($youtube_url, PHP_URL_QUERY), $query_string); | |
return $query_string["v"]; | |
endif; | |
$youtube_id = substr( $url['path'], 1 ); | |
if( strpos( $youtube_id, '/' ) ) | |
$youtube_id = substr( $youtube_id, 0, strpos( $youtube_id, '/' ) ); | |
return $youtube_id; | |
}else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment