Quick reference for Grabbing video IDs from Youtube and Vimeo.
*Note $string is the string containing youtube or vimeo link, can also contain rubbish.
** Note2: Video ID will be return in $output. Different PregMatch will result in Video ID being located differently in $output.
Youtube
There’s 2 formats, the latter youtu.be was launch fairly recently. Both formats uses the same video ID so it’s just 2 different extraction methods.
URL Format 1 (Traditional http://www.youtube.com/watch?v=zWw-823VTY0)
preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$string,$output);
$output[4][0] will contain the Video ID.
URL Format 2 (New http://youtu.be/asdasdasd)
preg_match_all('#(http://youtu.be/)([-|~_0-9A-Za-z]+)#i',$string,$output);
$output[2][0] will contain the Video ID.
Vimeo (http://player.vimeo.com/video/1234567)
preg_match_all('#(http://vimeo.com)/([0-9]+)#i',$string,$output);
$output[2][0] will contain the Video ID.
Nice one but i want to learn all preg_match function and i am not find any thing still 🙁
Hi, Altaf.
Watching Derek Banas’ awesome tutorial about RegEx on Youtube, then doing the exercises on RegexOne.com is how I had learnt.
Derek Banas: https://www.youtube.com/watch?v=DRR9fOXkfRE
RegexOne: regexone.com
Good luck.