Answers for "get channel id from youtube url"

0

get youtube id from url

<script type="text/javascript">
function youtube_parser(url){
    var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
    var match = url.match(regExp);
    return (match&&match[7].length==11)? match[7] : false;
}
</script>

These are the types of URLs supported

http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
http://www.youtube.com/v/0zM3nApSvMg?fs=1&amp;hl=en_US&amp;rel=0
http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
http://www.youtube.com/embed/0zM3nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM3nApSvMg
http://youtu.be/0zM3nApSvMg
Posted by: Guest on August-17-2020
0

get channel id from youtube url

function getYouTubeXMLUrl( $url, $return_id_only = false ) {

    $xml_youtube_url_base = 'https://www.youtube.com/feeds/videos.xml';
    $preg_entities        = [
        'channel_id'  => '\/channel\/(([^\/])+?)$', //match YouTube channel ID from url
        'user'        => '\/user\/(([^\/])+?)$', //match YouTube user from url
        'playlist_id' => '\/playlist\?list=(([^\/])+?)$',  //match YouTube playlist ID from url
    ];


    foreach ( $preg_entities as $key => $preg_entity ) {
        if ( preg_match( '/' . $preg_entity . '/', $url, $matches ) ) {
            if ( isset( $matches[1] ) ) {
                if($return_id_only === false){
                    return $xml_youtube_url_base . '?' . $key . '=' . $matches[1];
                }else{
                    return [
                        'type' => $key,
                        'id' => $matches[1],
                    ];
                }

            }
        }
    }

}

$url     = 'https://www.youtube.com/channel/UCBLAoqCQyz6a0OvwXWzKZag';
$xml_url = getYouTubeXMLUrl($url);

echo $xml_url; //outputs https://www.youtube.com/feeds/videos.xml?channel_id=UCBLAoqCQyz6a0OvwXWzKZag

$entity_id_array =  getYouTubeXMLUrl($url, true);

print_r($entity_id_array); // outputs Array ( [type] => channel_id [id] => UCBLAoqCQyz6a0OvwXWzKZag );
Posted by: Guest on October-18-2021

Code answers related to "get channel id from youtube url"

Browse Popular Code Answers by Language