Youtube & Vimeo URL preg_match



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.

2 Comments Say Something

Reply To Altaf Hussain Patel × Cancel Reply