How to get ID from Youtube Video using PHP

Using PHP code you can easily extract video ID of any youtube video. You can feed the input as the youtube video url and it can be processed easily using PHP. This tutorial helps you tu o process normal and shortened youtube urls to get video ID.

Youtube video urls can of these types:

Shortened Youtube Video URL

<https://youtu.be/-RdRvv60X2k>

Normal Youtube Video URL

<https://www.youtube.com/watch?v=-RdRvv60X2k>

Usually the video url can of the above two types. And we need to extract the video ID from both types of url inputs.

For both the video ID is:
-RdRvv60X2k

PHP CODE for Youtube Video ID extraction from Video URL

<?php
$video_url = 'https://youtu.be/-RdRvv60X2k'; //pass the url here
$substr = "=";
 
if (strpos($str, $substr) !== false) {
    $url_arr = explode("=",$video_url);
    $video_id = $url_arr[1];
    echo $video_id;
}else{
     $url_arr = explode("/",$video_url);
     $video_id = $url_arr[3];
     echo $video_id;
}
?>
Output: -RdRvv60X2k

Tags: Get video id from youtube, youtube video Id extraction, get id from youtube shortened url, youtube video link processing.

You May Also Like

About the Author: Sreeraj Melath