L
L
Leks22019-04-04 14:05:20
Java
Leks2, 2019-04-04 14:05:20

How to get youtube video link id?

How can links like https://youtu.be/KYukvjnHxo and https://youtube.com/watch?v=KYukvjnHxo pull the video id and put it into a String variable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem, 2019-04-04
@burlaka

I am using this design. Parses all address options

if (preg_match("/^((https?:\/\/)?(w{0,3}\.)?youtu(\.be|(be|be-nocookie)\.\w{2,3}\/))((watch\?v=|v|embed)?[\/]?(?P<video>[a-zA-Z0-9-_]{11}))/si", $link, $matches)) {
      return $matches['video'];
    }

C
Crash XD, 2019-04-05
@crashxd

I'm using this helper:

function getYoutubeID($link)
{
    preg_match('#(\.be/|/embed/|/v/|/watch\?v=)([A-Za-z0-9_-]{5,11})#', $link, $matches);
    if(isset($matches[2]) && $matches[2] != ''){
        return $matches[2];
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question