How to extract video ID from youtube urls?

Asked

Viewed 812 times

1

I’m a beginner in php and my problem is this.

This is the code currently

$link = https://www.youtube.com/watch?v=rW-44KuoAdA
 $id = explode('?v=', $link);
$get_video_info = 'http://www.youtube.com/get_video_info?&video_id='.$id[1].'&asv=3&el=detailpage&hl=en_US';
$get_video_info = curl($get_video_info);
$thumbnail_url = $title = $url_encoded_fmt_stream_map = $type = $url = '';

I need you to blow it up:

$link = https://www.youtube.com/watch?v=rW-44KuoAdA ou https://youtu.be/rW-44KuoAdA
 $id = explode('?v=', $link);
$get_video_info = 'http://www.youtube.com/get_video_info?&video_id='.$id[1].'&asv=3&el=detailpage&hl=en_US';
$get_video_info = curl($get_video_info);

How can I do that?

In case to make the explode recognize two different Urls.

@Edit

The two answers worked but I solved the problem like this.

    if( preg_match('/^https:\/\/w{3}?.youtube.com\//', $my_id) ){
        $url   = parse_url($my_id);
        $my_id = NULL;
        if( is_array($url) && count($url)>0 && isset($url['query']) && !empty($url['query']) ){
            $parts = explode('&',$url['query']);
            if( is_array($parts) && count($parts) > 0 ){
                foreach( $parts as $p ){
                    $pattern = '/^v\=/';
                    if( preg_match($pattern, $p) ){
                        $my_id = preg_replace($pattern,'',$p);
                        break;
                    }
                }
            }
            if( !$my_id ){
                echo '<p>No video id passed in</p>';
                exit;
            }
        }else{
            echo '<p>Invalid url</p>';
            exit;
        }
    }elseif( preg_match('/^https?:\/\/youtu.be/', $my_id) ) {
        $url   = parse_url($my_id);
        $my_id = NULL;
        $my_id = preg_replace('/^\//', '', $url['path']);
    }
} else {
    echo '<p>No video id passed in</p>';
    exit;
}
  • Do you want to add the .com.br? or just remove the . com and let . br?

  • Here is BR, so what you want to do with this code may have another function more suitable than the explode()

  • explode? why don’t you try using the implode?

  • I have a field where I put a link and it explodes takes for me what has after the . with/ I would like it when I put a link .com.br/ it caught too.

  • Shows an example of ai link

  • 1

    Another alternative - better in my view - would be to use parse_url to catch the path of the url.

  • 1

    @Papacharlie too mistrust

  • @rray, I even removed my previous comment, it looked like a gambiarra after remembering the parse_url. rs

  • $link = $my_id; $id = explode('? v=', $link); $get_video_info = 'http://www.youtube.com/get_video_info?&video_id='. $id[1]. '&asv=3&el=detailpage&hl=en_US';&#xA;$get_video_info = curl($get_video_info);&#xA;$thumbnail_url = $title = $url_encoded_fmt_stream_map = $type = $url = '';&#xA;parse_str($get_video_info);

  • What is that code?

  • a youtube Obs downloadable script: part of it

  • Is he part of the question or is that how he solved the problem?

  • is part of the question only that the problem still persists, was more to illustrate the problem, I want the explode return me a value of two different urls and do not know how to do.

  • 1

    Edit the question and put this code, if you have any more explanation tbm add

  • What you intend to do you won’t get by using only explode(). The question is focused on function explode(). The right thing would be to ask how to get the video id from youtube Urls, including different short URL and domains. This is more complex than simple explode().

Show 10 more comments

2 answers

1


Code removed from SO-en:

if (preg_match('/youtube\.com\/watch\?v=([^\&\?\/]+)/', $url, $id)) {
  $values = $id[1];
} else if (preg_match('/youtube\.com\/embed\/([^\&\?\/]+)/', $url, $id)) {
  $values = $id[1];
} else if (preg_match('/youtube\.com\/v\/([^\&\?\/]+)/', $url, $id)) {
  $values = $id[1];
} else if (preg_match('/youtu\.be\/([^\&\?\/]+)/', $url, $id)) {
  $values = $id[1];
}
else if (preg_match('/youtube\.com\/verify_age\?next_url=\/watch%3Fv%3D([^\&\?\/]+)/', $url, $id)) {
    $values = $id[1];
} else {   
// not an youtube video
}

original: https://stackoverflow.com/questions/9594943/regex-pattern-to-get-the-youtube-video-id-from-any-youtube-url

0

I created a function for your problem, study it, and can implement more url formats by adding after the if/elses in case I created only for your 2 url formats case.

 <?php

    function PegarIDYoutube($link) {
       // Explode formato ?v
       $link_formato_1 = explode("?v",$link);
       // Checa se o formato ?v existe se existir retorna o id
       if(is_array($link_formato_1)) {
          return $link_formato_1[1];
       // Senao ele tenta outro formato
       } else {
          // Explode formato .be/
          $link_formato_2 = explode(".be/",$link);

          // Checa se o formato link e .be se existir retorna o id
          if(is_array($link_formato_2)) {
            return $link_formato_2[1];
          // se não retorna false (link nao existente);
          } else {
            return false;
          }
       }

    }

 ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.