How do I extract the video ID of youtube urls?

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 the explode to do this:

$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 this?

In case the explode recognizes two different URLs.

@Edit

The two answers worked, but I solved the problem as well.

    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;
}
    
asked by anonymous 17.02.2016 / 21:57

2 answers

2

Code removed from SO-in:

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: link

    
18.02.2016 / 04:02
0

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

 <?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;
          }
       }

    }

 ?>
    
18.02.2016 / 04:00