Created
July 7, 2014 16:42
-
-
Save fer-ri/5428f9bb89d5c9e3b2c8 to your computer and use it in GitHub Desktop.
Extract Youtube ID From Any 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 | |
| $your_url='http://www.youtube.com/watch?var1=blabla#v=GvJehZx3eQ1$var2=bla'; | |
| function get_youtube_id_from_url($url){ | |
| if (stristr($url,'youtu.be/')) | |
| { preg_match('/(https:|http:|)(\/\/www\.|\/\/|)(.*?)\/(.{11})/i', $url, $final_ID); return $final_ID[4]; } | |
| else | |
| { preg_match('/(https:|http:|):(\/\/www\.|\/\/|)(.*?)\/(embed\/|watch\?v=|(.*?)&v=|v\/|e\/|.+\/|watch.*v=|)([a-z_A-Z0-9]{11})/i', $url, $IDD); return $IDD[6]; } | |
| } | |
| echo get_youtube_id_from_url($your_url); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment