Jump to content

Recommended Posts

I'm building a program in php that will be able to view YouTube videos from the URL. So if I type:

 

http://www.youtube.com/watch?v=(Video ID)

 

Then my program works and it will return the Video ID! But then some idiot clicks on a related video and this URL is generated:

 

http://www.youtube.com/watch?v=(Video ID)&feature=related

 

And my program strips the first bit by replacing "http://www.youtube.com/watch?v=" with an empty string "" but then I'm still left with the "&feature=related" I thought about just replacing that with an empty string as well but sometimes there can be a URL like this:

 

&feature=g-vrec&context=G28d9eecRVAAAAAAAABA

 

Which has a different unique code each time.

 

So I thought it'd be much simpler if I could use $_GET[] with an external URL, so the user types in:

 

http://www.youtube.com/watch?v=(Video ID)&feature=related

 

It just gets the "v" value rather than my buggy replace thing.

 

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/254430-_get-for-external-url/
Share on other sites

Thanks Dan, I haven't ever seen that function before. On the link that you posted though it contains at least 100 examples and I'm not going to go through them all. I also noticed your sig and I completely agree with you, I don't want to be a "Help Vampire" but could you please fix this example below since it's so small and simple or at least give me another hint ;)

 

$address = "http://www.youtube.com/watch?v=1234";

echo parse_url($address, "v");

 

I need it to echo "1234"

 

Thanks again.

I would use substr as the "v" of the query string will always be 11 characters long.

 

Something like this would work:

<?PHP

  $address = "http://www.youtube.com/watch?v=0x7EnB_uHMs&feature=related";
  $address = parse_url($address);
  $address = substr($address['query'],2,11);

  echo $address;

?>

 

Regards, PaulRyan.

v is not guaranteed to be a specific length under certain conditions.

 

YIAV, reading the manual is the only way to learn.  Will you remember anything about this thread in 3 months?  Probably not.  Would you remember if you had read the manual page with all the examples and details?  probably.

 

Also, there's only one official example in the manual, the rest are comments.  Read until the comments end, that's all you usually need.

 

$address = "http://www.youtube.com/watch?v=1234";
$a = array();
parse_str(parse_url($address, PHP_URL_QUERY), $a);
echo $a['v'];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.