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?
– Guilherme SpinXO
Here is BR, so what you want to do with this code may have another function more suitable than the
explode()
– rray
explode
? why don’t you try using theimplode
?– Guilherme SpinXO
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.
– Rafael Oliveira
Shows an example of ai link
– rray
Another alternative - better in my view - would be to use
parse_url
to catch thepath
of the url.– Papa Charlie
@Papacharlie too mistrust
– rray
@rray, I even removed my previous comment, it looked like a gambiarra after remembering the
parse_url
. rs– Papa Charlie
$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';
$get_video_info = curl($get_video_info);
$thumbnail_url = $title = $url_encoded_fmt_stream_map = $type = $url = '';
parse_str($get_video_info);
– Rafael Oliveira
What is that code?
– rray
a youtube Obs downloadable script: part of it
– Rafael Oliveira
Is he part of the question or is that how he solved the problem?
– rray
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.
– Rafael Oliveira
Edit the question and put this code, if you have any more explanation tbm add
– rray
What you intend to do you won’t get by using only
explode()
. The question is focused on functionexplode()
. 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 simpleexplode()
.– Daniel Omine