How to get full URL with php?

Scorpion Ghost

Elite Member
Executive VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
9,142
Reaction score
10,489
I have an area on my website where I can insert a Youtube (or any) link. Then another area that fetches what I inserted, but only fetches the ID of the link (the part after ?v= on Youtube videos). I'm certain this code does it:

Code:
$exp = explode("?v=",$video);

What I need is for this to not fetch the ID of the link, but to fetch the Entire link I insert.

Here's a Yotutube video - https://www.youtube.com/watch?v=787mIhI6Bko

The code I inserted above will fetch that Bolded ID bit. How can I make it fetch the Entire link?

Sorry for the noob question. Thanks :)
 
Code:
$exp = $video;

This will assign the entire url to the $exp without splitting/exploding the string/link at "?v="

You probably have another line of code under like $exp[1].

Just delete the square brackets so its just $exp.
 
Code:
$exp = $video;

This will assign the entire url to the $exp without splitting/exploding the string/link at "?v="

You probably have another line of code under like $exp[1].

Just delete the square brackets so its just $exp.

Bro, happy you're here. Check it out. I've been busting my head for hours to figure it out:

Code:
  // stugg here
               $vid = 0;
               for($i=1;$i<=3;$i++)
               {
                   $video = get_post_meta(get_the_ID(),'youtube_link'.$i,true);
                   if(strstr($video,"?v=") != false)
                   {
                       $exp = explode("?v=",$video);
                       $code_here = $exp[1];
                       $done = 1;
                  
                   }
                  
                   if(strstr($video,"youtu.be/") != false)
                   {
                       $exp = explode("youtu.be/",$video);
                       $code_here = $exp[1];
                       $done = 1;
                   }
                  
                   if(!empty($video) && $done == 1)
                   {
                  
                       echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="https://www.youtube.com/embed/'.$code_here.'"
                        frameborder="0" allowfullscreen></iframe> ';
                        $vid = 1;
                  
                   }
               }
              
               if($vid == 0) _e('No videos attached with this service.','PricerrTheme');
              
               ?>
              
              
          
           </div>

This basically embeds a Youtube video. What happens is the code takes the ID of the video i insert, and then cuts off the ID, and uses the ID for here:

Code:
                       echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="https://www.youtube.com/embed/'.$code_here.'"
                        frameborder="0" allowfullscreen></iframe> ';
                        $vid = 1;

I want to change this bit to this:

Code:
                       echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="'.$code_here.'"
                        frameborder="0" allowfullscreen></iframe> ';
                        $vid = 1;

But to make it successful I need that other part ($exp, $video, etc) to not pull only the ID, but to pull the entire video link that I will insert.

Any ideas how to make this work?
 
Try replacing this code :

PHP:
$video = get_post_meta(get_the_ID(),'youtube_link'.$i,true);
                   if(strstr($video,"?v=") != false)
                   {
                       $exp = explode("?v=",$video);
                       $code_here = $exp[1];
                       $done = 1;
                  
                   }
                  
                   if(strstr($video,"youtu.be/") != false)
                   {
                       $exp = explode("youtu.be/",$video);
                       $code_here = $exp[1];
                       $done = 1;
                   }

with

PHP:
$video = get_post_meta(get_the_ID(),'youtube_link'.$i,true);
                   if(strstr($video,"?v=") != false)
                   {
                       $exp = $video;
                       $code_here = $exp;
                       $done = 1;
                  
                   }
                  
                   if(strstr($video,"youtu.be/") != false)
                   {
                      
                       $exp = $video;
                       $code_here = $exp;
                       $done = 1;
                   }
 
Try replacing this code :

PHP:
$video = get_post_meta(get_the_ID(),'youtube_link'.$i,true);
                   if(strstr($video,"?v=") != false)
                   {
                       $exp = explode("?v=",$video);
                       $code_here = $exp[1];
                       $done = 1;
                 
                   }
                 
                   if(strstr($video,"youtu.be/") != false)
                   {
                       $exp = explode("youtu.be/",$video);
                       $code_here = $exp[1];
                       $done = 1;
                   }

with

PHP:
$video = get_post_meta(get_the_ID(),'youtube_link'.$i,true);
                   if(strstr($video,"?v=") != false)
                   {
                       $exp = $video;
                       $code_here = $exp;
                       $done = 1;
                 
                   }
                 
                   if(strstr($video,"youtu.be/") != false)
                   {
                     
                       $exp = $video;
                       $code_here = $exp;
                       $done = 1;
                   }

I literally did that before you told me to (based on your first reply). But no, when I do that it refuses to display anything on the site.
 
I literally did that before you told me to (based on your first reply). But no, when I do that it refuses to display anything on the site.

How about

PHP:
                       echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="https://www.youtube.com/embed/'.$video.'"
                        frameborder="0" allowfullscreen></iframe> ';
                        $vid = 1;
 
This code works for Youtube videos:

Code:
                    $video = get_post_meta(get_the_ID(),'youtube_link'.$i,true);
                    if(strstr($video,"?v=") != false)
                    {
                        $exp = explode("?v=",$video);
                        $code_here = $exp[1];
                        $done = 1;
                   
                    }
                   
                    if(strstr($video,"youtu.be/") != false)
                    {
                        $exp = explode("youtu.be/",$video);
                        $code_here = $exp[1];
                        $done = 1;
                    }
                   
                    if(!empty($video) && $done == 1)
                    {
                   
                        echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="https://www.youtube.com/embed/'.$code_here.'"
                         frameborder="0" allowfullscreen></iframe> ';
                         $vid = 1;

This code works for Dailymotion videos:

Code:
                    $video = get_post_meta(get_the_ID(),'youtube_link'.$i,true);
                    if(strstr($video,"/video/") != false)
                    {
                        $exp = explode("/video/",$video);
                        $code_here = $exp[1];
                        $done = 1;
                   
                    }
                   
                    if(strstr($video,"youtu.be/") != false)
                    {
                        $exp = explode("youtu.be/",$video);
                        $code_here = $exp[1];
                        $done = 1;
                    }
                   
                    if(!empty($video) && $done == 1)
                    {
                   
                        echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="https://www.dailymotion.com/embed/video/'.$code_here.'"
                         frameborder="0" allowfullscreen></iframe> ';
                         $vid = 1;

Look at the change. It's literally just a small tiny baby change. This /video/ instead of ?v= in 2 places, and this https://www.youtube.com/embed/'.$code_here. to this https://www.dailymotion.com/embed/video/'.$code_here.'

I just want to make it work for both :)
 
Last edited:
How about

PHP:
                       echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="https://www.youtube.com/embed/'.$video.'"
                        frameborder="0" allowfullscreen></iframe> ';
                        $vid = 1;

Trying...
 
I tried multiple times, with variations to $exp and the embed link, but it won't work.
 
How about

PHP:
                       echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="https://www.youtube.com/embed/'.$video.'"
                        frameborder="0" allowfullscreen></iframe> ';
                        $vid = 1;

This code will output https://www.youtube.com/embed/ + video url.

Delete that part so it looks like

PHP:
                       echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="'.$video.'"
                        frameborder="0" allowfullscreen></iframe> ';
                        $vid = 1;

If that doesn't work as well, shoot me a PM with your Teamviewer.
 
This code will output https://www.youtube.com/embed/ + video url.

Delete that part so it looks like

PHP:
                       echo '<iframe style="margin-right:10px;margin-bottom:10px;" width="358" height="250" src="'.$video.'"
                        frameborder="0" allowfullscreen></iframe> ';
                        $vid = 1;

If that doesn't work as well, shoot me a PM with your Teamviewer.

Doesn't work. I tried it with this $exp = $video; in both places, and I tried with this $exp = explode("?v=",$video); and $exp = explode("youtu.be/",$video);. Won't work.

Sending you a PM.
 
Have you tried printing the output of $video to ensure you are getting the data as expected in first place?

- edit, guess you solved it already... nvm :)
 
Back
Top