youtube url regexp(유튜부 url 정규식) > knowledge

본문 바로가기

사이트 내 전체검색

knowledge

youtube url regexp(유튜부 url 정규식)

작성일 18-09-11 10:56

페이지 정보

작성자 웹지기 조회 7,514회 댓글 0건

본문

YouTube URL PHP Regexp Shit


<?php

/**

 * YouTube Preg Match Test Case.

 *

 * @author Mario "Kuroir" Ricalde

 */


// Regular Expression (the magic).

$youtube_regexp = "/^http:\/\/(?:www\.)?(?:youtube.com|youtu.be)\/(?:watch\?(?=.*v=([\w\-]+))(?:\S+)?|([\w\-]+))$/";


// The test urls, one per line.

$urls = <<<EOF

http://www.youtube.com/watch?v=4-iI6UnKUCs&feature=grec_index

http://www.youtube.com/watch?v=4-iI6UnKUCs

http://www.youtube.com/watch?v=QNnz_ktVggQ&NR=1

http://youtu.be/QNnz_ktVAA

http://youtu.x

EOF;


// Turn each line into one single element in an array.

$urls = explode("\n", $urls);


foreach ($urls as $url) {

// Match a URL.

preg_match($youtube_regexp, $url, $matches);

// Remove empty values from the array (regexp shit).

$matches = array_filter($matches, function($var) {

return($var !== '');

});

// If we have 2 elements in array, it means we got a valid url!

// $matches[2] is the youtube ID!

if (sizeof($matches) == 2) {

var_dump($matches);

}

}

----------

try this one instead. works fine in this some urls

https://youtube.com/watch?v={video_id}&param1={value}

https://youtube.com/watch?v={video_id}

$pattern = '%^(?:https?://)?(?:www.)?(?:youtu.be/|youtube.com(?:/embed/|/v/|/watch?v=))([\w-]{10,12})(?:\S+)$%x';


----------

%^(?:https?://)?(?:www.)?(?:youtu.be/|youtube.com(?:/embed/|/v/|/watch?v=))([\w-]{10,12})(?:\S+)$%x will not validate

http://www.youtube.com/watch?v=8F5VSKT38r0

correctly.

MATCH 1


----------

I add "https://" for the start of string

/^(http://|https://)(?:www.)?(?:youtube.com|youtu.be)/(?:watch?(?=.*v=([\w-]+))(?:\S+)?|([\w-]+))$/

[이 게시물은 웹지기님에 의해 2018-09-23 13:28:50 Html에서 이동 됨]

추천0

비추천 0

댓글목록

등록된 댓글이 없습니다.

전체 89건 2 페이지

이미지 목록

게시물 검색
Copyright © 즐거운 코딩 생활 ( funyphp ). All rights reserved.
PC 버전으로 보기