Skip to content

Instantly share code, notes, and snippets.

@jonnymaceachern
Last active July 17, 2017 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnymaceachern/0a9ef7f7a7789402ca4c to your computer and use it in GitHub Desktop.
Save jonnymaceachern/0a9ef7f7a7789402ca4c to your computer and use it in GitHub Desktop.
Get YouTube ID by URL
<?php
function get_youtube_id( $url ) {
preg_match( "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches );
return $matches[0];
}
// Test
echo get_youtube_id( "https://youtu.be/MZj1bQtRNdk" ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment