Last active
July 17, 2017 16:50
-
-
Save jonnymaceachern/0a9ef7f7a7789402ca4c to your computer and use it in GitHub Desktop.
Get YouTube ID by 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
<?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