$re = '/(?:(.*)(?:https:|http:)?\/\/)?(?:(.*)(?:www\.youtube\.com\/(?:embed\/|watch\?v=)|youtu\.be\/|youtube\.googleapis\.com\/v\/)(?<YoutubeID>[a-z0-9-_]{11,12})|(?:vimeo\.com\/|player\.vimeo\.com\/video\/)(?<VimeoID>[0-9]+))/mi';
$str = 'https://www.youtube.com/embed/123456789012
https://www.youTUbe.com/embed/123456789012
http://www.youtube.com/watch?v=My2FRPA3Gf8
htTp://www.youtube.com/watch?V=My2FRPA3Gf8
http://youtu.be/My2FRPA3Gf8
http://youTU.be/My2FRPA3Gf8
https://www.youtu.be/embed/123456789012
https://youtube.googleapis.com/v/123456789012
https://youtube.googleAPis.com/v/123456789012
https://youtube.googleapis.com/embed/123456789012
https://www.youtube.com/embed/123456789012
http://www.youtube.com/embed/123456789012
//www.youtube.com/embed/123456789012
www.youtube.com/embed/123456789012
http://vimeo.com/25451551
http://player.vimeo.com/video/25451551
https://player.vimeo.com/video/6969232737373733383782383273287328327342873
http://player.vimeo.com/video/6969
http://plaser.vimeo.com/video/6969
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php