Get Youtube Video ID from URL

Each Youtube video gets a unique id which helps the site identify and render its content. This page details the method to get Youtube Video ID from URL. In order to display a video on our custom site, we can use the embed code provided by youtube on every video. This is available by -

get youtube video id from url

  • Clicking on "Share link".
  • Click on "Embed Code".
  • Paste the code provided by youtube on our HTML code snippet.

Get Youtube Video ID from URL

Below code can help user retrieve the unique id of the video and use it elsewhere on our site, store it in db, etc.

* Extract video id from youtube URL.
* @param string $url
*  - Youtube URL in the form of "" or ""
public function _get_youtube_id($url) {
  list($dontCare, $videoID) = explode('v=', $url);

  //To be very precise, we can use preg_match as below -
  $extractURLMatch = preg_match('![?&]{1}v=([^&]+)!', $url . '&', $data);
  if ($extractURLMatch){
    $videoID = $data[1];

  //check if url is a short url provided by youtube.
  if (strpos($url, '') != FALSE) {
    $videoID = substr($url, strpos($url, "") + 9);
  return $videoID;

Param "$url" can be a complete youtube video URL or the short URL of the site.