How I can take a special part from a url ? For example i want to take only v=q07isX-Q1-U from this url http://m.youtube.com/watch?gl=US&hl=en-GB&client=mv-google&feature=m-featured&v=q07isX-Q1-U . Is it possible in php.
Asked
Active
Viewed 86 times
3 Answers
2
parse_str(parse_url($url, PHP_URL_QUERY), $values);
if (isset($values['v'])) {
echo $values['v'];
}

deceze
- 510,633
- 85
- 743
- 889
-
-
Well, please read the manual. `parse_url` parses the URL into separate pieces, `parse_str` parses the query string part of the URL into separate values. – deceze May 02 '12 at 04:10
-
Thanks for reply. One more question can i use $values['v'] directly in function & print because the url will be $_POST to this page. – Jibon May 02 '12 at 04:15
-
Sorry, not getting what you're asking. `$values['v']` will be a regular variable like any other variable. – deceze May 02 '12 at 04:20
-
whatever variable you want to put in can be there as the 2nd parameter for parse_str(). doesn't have to be $values. – Jim Michaels May 02 '12 at 06:25
0
<?php echo $_GET["v"]; ?>
This should return what is stored in the v= parameter
EDIT: I should note that this only works if your script is receiving the parameters as part of the get request. Not if you are trying to just parse it as text.

Andrew Landsverk
- 673
- 7
- 17
0
<?php
$url = "http://m.youtube.com/watch?gl=US&v=q07isX-Q1-U";
print_r(parse_url($url));
$str= parse_url($url, PHP_URL_PATH);
parse_str($str);
echo $gl; // US
echo $v; // q07isX-Q1-U
parse_str($str, $output);
echo $output['gl']; // US
echo $output['v']; // q07isX-Q1-U
?>

Amit Kumar Khare
- 565
- 6
- 17