Skip to content

Instantly share code, notes, and snippets.

@fer-ri
Created July 7, 2014 16:42
Show Gist options
  • Select an option

  • Save fer-ri/5428f9bb89d5c9e3b2c8 to your computer and use it in GitHub Desktop.

Select an option

Save fer-ri/5428f9bb89d5c9e3b2c8 to your computer and use it in GitHub Desktop.
Extract Youtube ID From Any URL
<?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