Created
January 23, 2017 22:31
-
-
Save galengidman/aa9f08d15275e4b9e223890bc5bdd122 to your computer and use it in GitHub Desktop.
Get a YouTube video ID from any format of YouTube URL.
This file contains hidden or 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
<?php | |
function youtube_id_from_url( $url = null ) { | |
if ( ! $url || ( stripos( $url, 'youtube.com' ) === false && stripos( $url, 'youtu.be' ) === false ) ) { | |
return false; | |
} | |
$parsed_url = parse_url( $url ); | |
if ( stripos( $url, '?v=' ) !== false ) { | |
parse_str( $parsed_url['query'] ); | |
$id = $v; | |
} else { | |
$id = end( explode( '/', $parsed_url['path'] ) ); | |
} | |
return $id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment