사이트 내 전체검색
검색목록
유투브 동영장 주소 추출 PHP 정규식 예 2 (parse_str 방식)
로빈아빠
https://cmd.kr/javascript/853 URL이 복사되었습니다.

본문

<?
$url= "http://www.youtube.com/watch?ist=PL0E8ECDEA23DA6D15&feature=player_detailpage&v=zCSQE5Lwk2s"; 

preg_match("/https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:['\"][^<>]*>|<\/a>))[?=&+%\w-]*/is", $url, $mat);
var_dump($mat);


$pattern = '/(?:(?:\?|&)v=|\/)([A-Za-z0-9]{11})/';$url = 'http://www.youtube.com/watch?v=KMU0tzLwhbE';preg_match($pattern, $url, $matches);echo $matches[1];

$video_url = "http://www.youtube.com/watch?v=KMU0tzLwhbE"; 
// http://youtu.be/KMU0tzLwhbE
$url_parts = parse_url($video_url);
if (isset($url_parts["query"]) && (strpos($url_parts["query"], "v") !== false)) 
 parse_str($url_parts["query"], $vars);
 // Handle full URLs with query string like 'http://www.youtube.com/watch?v=KMU0tzLwhbE' 
if (isset($vars["v"]) && $vars["v"]) 
{
 $video_code = $vars["v"]; // Handle the new short URLs like 'http://youtu.be/KMU0tzLwhbE' 
} else if ($url_parts['path'])
 { 
 $video_code = trim($url_parts['path'], '/');
 }
}


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.



// ------------------- 아래는 parse_str 방식 추출

parse_str(parse_url($url, PHP_URL_QUERY ), $youtubeURL);
echo $youtubeURL['ist'];
echo $youtubeURL['v'];

?>


댓글목록

등록된 댓글이 없습니다.

21 (1/1P)

Search

Copyright © Cmd 명령어 3.145.155.91