Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.