$re = '~^(https?://)?(www\.)?youtu(\.be|be\.com)/(watch\?v=|embed/)?(?<id>[\w-]{11,})~m';
$str = 'https://youtube.com/watch?v=q9s_9RZ6lvw
https://www.youtube.com/watch?v=q9s_9RZ6lvw
https://youtu.be/q9s_9RZ6lvw
https://www.youtube.com/embed/q9s_9RZ6lvw
https://youtube.com/embed/q9s_9RZ6lvw
youtube.com/watch?v=q9s_9RZ6lvw
www.youtube.com/watch?v=q9s_9RZ6lvw
youtu.be/q9s_9RZ6lvw
youtube.com/embed/q9s_9RZ6lvw
http://youtube.com/watch?v=q9s_9RZ6lvw
http://www.youtube.com/watch?v=q9s_9RZ6lvw
http://youtu.be/q9s_9RZ6lvw
http://www.youtube.com/embed/q9s_9RZ6lvw
http://youtube.com/embed/q9s_9RZ6lvw
http://youtube.com/watch?v=q9s_9RZ6lvw&rel=1
http://www.youtube.com/watch?v=q9s_9RZ6lvw&autoplay=1
http://youtu.be/q9s_9RZ6lvw?ref=0
http://www.youtube.com/embed/q9s_9RZ6lvw?abc=xyz
http://youtube.com/embed/q9s_9RZ6lvw?1';
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