javascript - Regex pattern to extract domain name, video url and video id from a string containing youtube/vimeo url with PHP -
i need regex or php preg_match function should extract youtube/vimeo url , video provider/domain name (vimeo/youtube) string containing video url.
and extracted video url of string, need find exact video id.
the regex should graph video id below url also,
thanks, working on solution. post answer if find it.
$sample_text = "cieker largest talentize social , professional networking website, can view on https://www.cieker.com , video on https://www.youtube.com/watch?v=jgyzdgpv_hk";
// function return video url string
function extract($html) { $regex = '/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|channels\/(?:\w+\/)|watch\?v=|v\/)?([a-za-z0-9._%-]*)(\&\s+)?/'; preg_match_all($regex, $html, $match); $matched = array_unique($match[0]); usort($matched, function($a, $b) { return strlen($b) - strlen($a); }); return $matched; }
// calls function, returns youtube or vimeo url string.
$check_extract = extract($sample_url );
// function find video provider name.
function videotype($url) { if (strpos($url, 'youtu') > 0) { return 'youtube'; } else if (strpos($url, 'vimeo') > 0) { return 'vimeo'; } else { return 'unknown'; } }
// calls function, has extracted url parameter.
$provider = videotype($check_extract[0]);
// following regex extract video id above extracted youtube url.
if($provider=="youtube") { preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/",$check_extract[0], $matches);?> $id =$matches[1]; } else if($provider=="vimeo") { preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/",$check_extract[0], $output_array);?> $id =$output_array[5]; }
// video id of youtube/vimeo.
$video_id = $id;
Comments
Post a Comment