Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6084311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:30:23+00:00 2026-05-23T11:30:23+00:00

I am coding a functionality that allows users to enter a Youtube video URL.

  • 0

I am coding a functionality that allows users to enter a Youtube video URL. I would like to extract the video ID from these urls.

Does Youtube API support some kind of function where I pass the link and it gives the video ID in return. Or do I have to parse the string myself?

I am using PHP … I would appreciate any pointers / code samples in this regard.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:30:23+00:00Added an answer on May 23, 2026 at 11:30 am

    Here is an example function that uses a regular expression to extract the youtube ID from a URL:

    /**
     * get youtube video ID from URL
     *
     * @param string $url
     * @return string Youtube video id or FALSE if none found. 
     */
    function youtube_id_from_url($url) {
        $pattern = 
            '%^# Match any youtube URL
            (?:https?://)?  # Optional scheme. Either http or https
            (?:www\.)?      # Optional www subdomain
            (?:             # Group host alternatives
              youtu\.be/    # Either youtu.be,
            | youtube\.com  # or youtube.com
              (?:           # Group path alternatives
                /embed/     # Either /embed/
              | /v/         # or /v/
              | /watch\?v=  # or /watch\?v=
              )             # End path alternatives.
            )               # End host alternatives.
            ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
            $%x'
            ;
        $result = preg_match($pattern, $url, $matches);
        if ($result) {
            return $matches[1];
        }
        return false;
    }
    
    echo youtube_id_from_url('http://youtu.be/NLqAF9hrVbY'); # NLqAF9hrVbY
    

    It’s an adoption of the answer from a similar question.


    It’s not directly the API you’re looking for but probably helpful. Youtube has an oembed service:

    $url = 'http://youtu.be/NLqAF9hrVbY';
    var_dump(json_decode(file_get_contents(sprintf('http://www.youtube.com/oembed?url=%s&format=json', urlencode($url)))));
    

    Which provides some more meta-information about the URL:

    object(stdClass)#1 (13) {
      ["provider_url"]=>
      string(23) "http://www.youtube.com/"
      ["title"]=>
      string(63) "Hang Gliding: 3 Flights in 8 Days at Northside Point of the Mtn"
      ["html"]=>
      string(411) "<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/NLqAF9hrVbY?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/NLqAF9hrVbY?version=3" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" allowfullscreen="true"></embed></object>"
      ["author_name"]=>
      string(11) "widgewunner"
      ["height"]=>
      int(344)
      ["thumbnail_width"]=>
      int(480)
      ["width"]=>
      int(425)
      ["version"]=>
      string(3) "1.0"
      ["author_url"]=>
      string(39) "http://www.youtube.com/user/widgewunner"
      ["provider_name"]=>
      string(7) "YouTube"
      ["thumbnail_url"]=>
      string(48) "http://i3.ytimg.com/vi/NLqAF9hrVbY/hqdefault.jpg"
      ["type"]=>
      string(5) "video"
      ["thumbnail_height"]=>
      int(360)
    }
    

    But the ID is not a direct part of the response. However it might contain the information you’re looking for and it might be useful to validate the youtube URL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask if there is a functionality in RadRails that allows
I have a search functionality that obtains data from an InnoDB table ( utf8_spanish_ci
I just finished coding a class, and realized that all of its public functionality
Our coding standards ask that we minimise the use of C# var (suggests limiting
I have a web app built in coldfusion that receives medical referral letters from
This is closely related to another question I asked: Is there functionality that is
I currently have a single application that needs to be started from a windows
I have a c# windows form application that has a similar GUI functionality as
Let's say that you have to implement some functionality that is not trivial (it
Currently i'm coding a webpage (or the functionality of a webpage) but the design

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.