regular expression for youtube and vimeo

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.

No Comments Say Something

Add A Comment

*

Home
Codes
06/29/2011
12:11 PM
Codes
01/29/2008
09:02 PM
Codes
10/26/2007
10:47 PM
Rants
08/15/2007
06:02 PM
Codes
06/28/2007
03:33 PM
Rants
06/27/2007
11:38 PM