neha4no
2/20/2019 - 11:10 AM

Regex to detect video Source between youtube and 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 ) 
====