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


Normal Youtube Video URL


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:

PHP CODE for Youtube Video ID extraction from Video URL

$video_url = ''; //pass the url here
$substr = "=";
if (strpos($str, $substr) !== false) {
    $url_arr = explode("=",$video_url);
    $video_id = $url_arr[1];
    echo $video_id;
     $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