html 正则表达式检测youtube和wistia之间的视频源

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 正则表达式检测youtube和wistia之间的视频源相关的知识,希望对你有一定的参考价值。

<?php
/**
 *  Function to detect video source (youtube & wistia)
 * @param type $url string
 * @return type $source string
 */
function get_video_src($url){
    if (empty($url)) {
        return;
    } 
    $source = '';
    preg_match('/(?:https:\/\/|http:\/\/)?(youtube|youtu\.be|wistia)(?:\.com)?\/*/', $url, $match);
    if (isset($match[1]) && !empty($match[1])) {
        $source = ($match[1] == 'youtu.be') ? 'youtube' : $match[1];
    }
    
    return $source;
}

function get_video_src_testing($url){
    if (empty($url)) {
        return;
    } 
    $array = array('https://www.youtube.com/watch?v=Q-nYYuq0-YM',
        'https://youtu.be/Q-nYYuq0-YM',
        'https://www.youtube.com/embed/Q-nYYuq0-YM',
        'www.youtube.com/embed/Q-nYYuq0-YM',
        'http://youtube.com/watch?v=iwGFalTRHDA',
        'http://www.youtube.com/watch?v=iwGFalTRHDA&feature=related',
        'http://www.youtube.com/watch?v=iwGFalTRHDA&feature=related',
        'https://www.youtube.com/channel/UCDZkgJZDyUnqwB070OyP72g',
        'https://youtube.com/iwGFalTRHDA',
        'http://youtu.be/n17B_uFF4cA',
        'youtube.com/n17B_uFF4cA',
        'http://www.youtube.com/embed/watch?feature=player_embedded&v=r5nB9u4jjy4',
        'http://www.youtube.com/watch?v=t-ZRX8984sc',
        'http://youtu.be/t-ZRX8984sc',
        'https://vwo.wistia.com/medias/vv8ife3d2n',
        'https://youtu.be/Q-nYYuq0-YM',
        'https://youtu.be/Q-nYYuq0-YM',
        'https://www.youtube.com/embed/Q-nYYuq0-YM',
        'https://support.wistia.com/medias/h1z3uqsjal',
        'https://support.wistia.com/medias/ktv95e6b2g?wtime=1m1s',
        'https://support.wistia.com/medias/h1z3uqsjal?wtime=2',
        );
    foreach($array as $key => $url){ 
        echo $key .' : '.$url .'<br>';
        preg_match('/(?:https:\/\/|http:\/\/)?(youtube|youtu\.be|wistia)(?:\.com)?\/*/', $url, $match);
        print_r($match); 
        echo '<br> ====<br>';
    }
   return;
}
0 : https://www.youtube.com/watch?v=Q-nYYuq0-YM
Array ( [0] => youtube.com/ [1] => youtube ) 
====
1 : https://youtu.be/Q-nYYuq0-YM
Array ( [0] => https://youtu.be/ [1] => youtu.be ) 
====
2 : https://www.youtube.com/embed/Q-nYYuq0-YM
Array ( [0] => youtube.com/ [1] => youtube ) 
====
3 : www.youtube.com/embed/Q-nYYuq0-YM
Array ( [0] => youtube.com/ [1] => youtube ) 
====
4 : http://youtube.com/watch?v=iwGFalTRHDA
Array ( [0] => http://youtube.com/ [1] => youtube ) 
====
5 : http://www.youtube.com/watch?v=iwGFalTRHDA&feature=related
Array ( [0] => youtube.com/ [1] => youtube ) 
====
6 : http://www.youtube.com/watch?v=iwGFalTRHDA&feature=related
Array ( [0] => youtube.com/ [1] => youtube ) 
====
7 : https://www.youtube.com/channel/UCDZkgJZDyUnqwB070OyP72g
Array ( [0] => youtube.com/ [1] => youtube ) 
====
8 : https://youtube.com/iwGFalTRHDA
Array ( [0] => https://youtube.com/ [1] => youtube ) 
====
9 : http://youtu.be/n17B_uFF4cA
Array ( [0] => http://youtu.be/ [1] => youtu.be ) 
====
10 : youtube.com/n17B_uFF4cA
Array ( [0] => youtube.com/ [1] => youtube ) 
====
11 : http://www.youtube.com/embed/watch?feature=player_embedded&v=r5nB9u4jjy4
Array ( [0] => youtube.com/ [1] => youtube ) 
====
12 : http://www.youtube.com/watch?v=t-ZRX8984sc
Array ( [0] => youtube.com/ [1] => youtube ) 
====
13 : http://youtu.be/t-ZRX8984sc
Array ( [0] => http://youtu.be/ [1] => youtu.be ) 
====
14 : https://vwo.wistia.com/medias/vv8ife3d2n
Array ( [0] => wistia.com/ [1] => wistia ) 
====
15 : https://youtu.be/Q-nYYuq0-YM
Array ( [0] => https://youtu.be/ [1] => youtu.be ) 
====
16 : https://youtu.be/Q-nYYuq0-YM
Array ( [0] => https://youtu.be/ [1] => youtu.be ) 
====
17 : https://www.youtube.com/embed/Q-nYYuq0-YM
Array ( [0] => youtube.com/ [1] => youtube ) 
====
18 : https://support.wistia.com/medias/h1z3uqsjal
Array ( [0] => wistia.com/ [1] => wistia ) 
====
19 : https://support.wistia.com/medias/ktv95e6b2g?wtime=1m1s
Array ( [0] => wistia.com/ [1] => wistia ) 
====
20 : https://support.wistia.com/medias/h1z3uqsjal?wtime=2
Array ( [0] => wistia.com/ [1] => wistia ) 
====

以上是关于html 正则表达式检测youtube和wistia之间的视频源的主要内容,如果未能解决你的问题,请参考以下文章

PHP Youtube视频ID正则表达式和PHP功能

C#使用正则表达式检测数字 char 和韩文

Youtube 完整的 Java 正则表达式

正则表达式从 youtube/vimeo url 中提取域和视频 ID

利用正则表达式实现python强口令检测

youtube URL 的正则表达式