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.


There’s 2 formats, the latter was launch fairly recently. Both formats uses the same video ID so it’s just 2 different extraction methods.

URL Format 1 (Traditional


$output[4][0] will contain the Video ID.

URL Format 2 (New


$output[2][0] will contain the Video ID.

Vimeo (


$output[2][0] will contain the Video ID.

2 Comments Say Something

Add A Comment