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