$sample_text = "Cieker is the largest talentize social and professional networking website, you can view it on https://www.cieker.com and the about video is on https://www.youtube.com/watch?v=jGyZDgpv_Hk";
//从字符串返回视频url的函数
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;
}
// 调用函数,从字符串中返回 youtube 或 vimeo url。
$check_extract = extract($sample_url );
// 查找视频提供商名称的函数。
function videoType($url) {
if (strpos($url, 'youtu') > 0)
{
return 'youtube';
}
else if (strpos($url, 'vimeo') > 0)
{
return 'vimeo';
}
else
{
return 'unknown';
}
}
//调用函数,已提取url作为参数。
$provider = videoType($check_extract[0]);
// 下面的正则表达式将从上面提取的 youtube url 中提取视频 id。
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];
}
// 这将获得 youtube/vimeo 的视频 ID。
$video_id = $id;