본문 바로가기

Story/php

유튜브 영상 주소에서 영상 아이디 값구하는 정규식 / parse youtube video id using preg_match

반응형

유튜브 영상 주소에서 영상 아이디 값구하는 정규식

parse youtube video id using preg_match

 

if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[\w\-?&!#=,;]+/[\w\-?&!#=/,;]+/|(?:v|e(?:mbed)?)/|[\w\-?&!#=,;]*[?&]v=)|youtu\.be/)([\w-]{11})(?:[^\w-]|\Z)%i', $url, $match)) {
    $video_id
= $match[1];
}

 

if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
    $video_id
= $match[1];
}

 

http://youtu.be/dQw4w9WgXcQ ...
http://www.youtube.com/embed/dQw4w9WgXcQ ...
http://www.youtube.com/watch?v=dQw4w9WgXcQ ...
http://www.youtube.com/?v=dQw4w9WgXcQ ...
http://www.youtube.com/v/dQw4w9WgXcQ ...
http://www.youtube.com/e/dQw4w9WgXcQ ...
http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcQ ...
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcQ ...
http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ ...
http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ ...

 

출처 : http://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match

반응형