...

View Full Version : parse_url not working



YourDirector
07-11-2012, 01:13 PM
Hey everyone,

I've used the following code on one one of my sites before to extract the video ID from a youtube URL:


$url=$vidlink;
parse_str( parse_url($url, PHP_URL_QUERY), $my_array_of_vars);
$vidcode=$my_array_of_vars['v'];
(Where $vidlink == the original url)

And this works brilliantly. However, I'm trying to use the exact same code on another site and it's just not working. It won't output anything. Does anyone know of a reason this wouldn't work?

Thanks

firepages
07-11-2012, 01:35 PM
whats the url ? if its seo-friendly then there will not be a query string to grab e.g. mysite.com/index/name-of-page/page1 will fail with your function call

if you show us the url we might be able to suggest some other method

YourDirector
07-11-2012, 02:04 PM
Any youtube URL, they generally follow the same standard format:
As an example, here's an example:

http://www.youtube.com/watch?v=_y9vzYDaKRY&feature=g-vrec

sunfighter
07-11-2012, 03:19 PM
You didn't give us $my_array_of_vars so not sure what your looking for but maybe something like this:


<?php
$url = 'http://www.youtube.com/watch?v=_y9vzYDaKRY&feature=g-vrec';
$sam = (parse_url($url));
echo $sam["query"];
?>

YourDirector
07-18-2012, 04:48 PM
You didn't give us $my_array_of_vars so not sure what your looking for but maybe something like this:


$my_array_of_vars is the array created by the 'parse_str' function so there is surely nothing more to give on that?

YourDirector
07-18-2012, 05:05 PM
In short I simply want to be able to take any url thats given and extract the variable with in it labled 'v'.

So for example:
"www.thisurl.com?v=ted"
would output "ted"

"www.thaturl.com?loc=bar&v=ball&steel=shed"
would output "ball"

YourDirector
07-18-2012, 06:23 PM
Found this. Bit more bulky but works like a dream:


function getVariableFromUrl($url) { //we need to see if this URL is passing any GET variables

$variablesStart = strpos($url, "?") + 1;

if (!$variablesStart) {

// no variables!

return(false);

}

//before we extract the variables, we need to remove any anchors

$variablesEnd = strpos($url,"#",$variablesStart);

if ($variablesEnd) {

$getVariables = substr($url, $variablesStart, $variablesEnd - $variablesStart);

} else {

$getVariables = substr($url, $variablesStart);

}

//next, we split the URL into an arrays containing variable name and value pairs (ie. "variable=value")

$variableArray = explode("&", $getVariables);

//we will iterate through each of the array pairs (ie. "variable=value")

foreach ($variableArray as $arraySet) {

$nameAndValue = explode("=", $arraySet);

//using the above examples, $nameAndValue[0] would be "variable" and $nameAndValue[1] would be "value"

$output[$nameAndValue[0]] = $nameAndValue[1];

}

return($output);

}

Fou-Lu
07-18-2012, 08:50 PM
Erm, I'm not quite sure I follow your problem here. Your original post works fine for me:


$url = 'http://www.youtube.com/watch?v=_y9vzYDaKRY&feature=g-vrec';
parse_str(parse_url($url, PHP_URL_QUERY), $a);
if (isset($a['v']))
{
printf('V has been supplied and it is: %s' . PHP_EOL, $a['v']);
}



V has been supplied and it is: _y9vzYDaKRY


Unless I misunderstand completely what you are doing, then your original post should work as expected. I tried it with these other two links you have, and they also worked fine.
What version of PHP are you using? Do you have more examples of something that's not working as expected (I do find that parse_url is somewhat picky at times, so this may be a version difference).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum