YIAV Posted January 5, 2012 Share Posted January 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254430-_get-for-external-url/ Share on other sites More sharing options...
ManiacDan Posted January 5, 2012 Share Posted January 5, 2012 Parse_url Quote Link to comment https://forums.phpfreaks.com/topic/254430-_get-for-external-url/#findComment-1304571 Share on other sites More sharing options...
YIAV Posted January 5, 2012 Author Share Posted January 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254430-_get-for-external-url/#findComment-1304592 Share on other sites More sharing options...
PaulRyan Posted January 5, 2012 Share Posted January 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254430-_get-for-external-url/#findComment-1304600 Share on other sites More sharing options...
ManiacDan Posted January 5, 2012 Share Posted January 5, 2012 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']; Quote Link to comment https://forums.phpfreaks.com/topic/254430-_get-for-external-url/#findComment-1304602 Share on other sites More sharing options...
YIAV Posted January 5, 2012 Author Share Posted January 5, 2012 Thanks to both of you! Dan your method works perfectly! As for your method Ryan, it should work but I preffer to use Dan's method; I've never liked messing around with strings that much. Quote Link to comment https://forums.phpfreaks.com/topic/254430-_get-for-external-url/#findComment-1304612 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.