Reputation: 104
I want to read one parameter from a specific URl :
Like : http://www.youtube.com/watch?v=MrOiL74P-9E&feature=watch
Output should be : MrOiL74P-9E
I try to search and I found this function :
function remove_query_part($url, $term)
{
$query_str = parse_url($url, PHP_URL_QUERY);
if ($frag = parse_url($url, PHP_URL_FRAGMENT)) {
$frag = '#' . $frag;
}
parse_str($query_str, $query_arr);
unset($query_arr[$term]);
$new = '?' . http_build_query($query_arr) . $frag;
return str_replace(strstr($url, '?'), $new, $url);
}
This function just remove one parameter and return the rest. Can anyone edit this function to return just the video ID and Ignore whatever else in the URL.
Upvotes: 1
Views: 77
Reputation: 1078
Not tested but you could try....
function remove_query_part($url, $term) {
// get the query part of the string (i.e. after the '?')
$query_str = parse_url($url, PHP_URL_QUERY);
$queryItems = explode('&', $query_str);
$item = array();
$itemArray = array();
foreach ($queryItems as $item) {
$itemArray = explode('=', $item);
if($item[0] == $term) {
return $item[1];
}
}
return false;
}
Upvotes: 2
Reputation: 971
Quick and dirty:
$url='http://www.youtube.com/watch?v=MrOiL74P-9E&feature=watch';
function test($url)
{
$data=parse_url($url);
if(!isset($data['query']))
{
return null;
}
else
{
$ex=explode('&', $data['query']);
foreach($ex as $key => $val)
{
$param=explode('=', $val);
if($param[0]=='v')
{
return $param[1];
break;
}
}
}
}
echo test($url);
Upvotes: 2
Reputation: 16709
$query_str = parse_url($url, PHP_URL_QUERY);
parse_str($query_str, $args);
print $args['v']; // <- MrOiL74P-9E
I think you know how to put this into a function...
Upvotes: 3