Skip to content

Instantly share code, notes, and snippets.

@galengidman
Created January 23, 2017 22:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save galengidman/aa9f08d15275e4b9e223890bc5bdd122 to your computer and use it in GitHub Desktop.
Save galengidman/aa9f08d15275e4b9e223890bc5bdd122 to your computer and use it in GitHub Desktop.
Get a YouTube video ID from any format of YouTube URL.
<?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