bftwofreak Posted August 30, 2008 Share Posted August 30, 2008 I'm trying to take the variable from a url in text form. ex.: <?php $url = http://www.example.com/test.php?foo=bar&test=1 I'm basically looking for a function or a way to extract the variables or at least one of them. I've tried extract, but for some reason it won't work... Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/ Share on other sites More sharing options...
dezkit Posted August 30, 2008 Share Posted August 30, 2008 elaborate plz Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629542 Share on other sites More sharing options...
bftwofreak Posted August 30, 2008 Author Share Posted August 30, 2008 I'm trying to find a function to specifically draw out the variables past the "?". for example the result would be: $foo = bar $test = 1 -----------------or-------------- $example['foo'] = bar $example['test'] = 1 Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629545 Share on other sites More sharing options...
ratcateme Posted August 30, 2008 Share Posted August 30, 2008 i wrote this a while ago function get_get($url) { $data = substr($url, strpos($url, '?') + 1); $data = explode('&', $data); foreach ($data as $value) { $value = explode('=', $value); $get[($value[0])] = $value[1]; } return $get; } Scott. Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629546 Share on other sites More sharing options...
bftwofreak Posted August 30, 2008 Author Share Posted August 30, 2008 didn't work... the output showed as just "$" (no quotes) Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629574 Share on other sites More sharing options...
ratcateme Posted August 30, 2008 Share Posted August 30, 2008 $url = "http://www.example.com/test.php?foo=bar&test=1"; function get_get($url) { $data = substr($url, strpos($url, '?') + 1); $data = explode('&', $data); foreach ($data as $value) { $value = explode('=', $value); $get[($value[0])] = $value[1]; } return $get; } print_r(get_get($url)); outputs Array ( [foo] => bar [test] => 1 ) Scott. Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629576 Share on other sites More sharing options...
bftwofreak Posted August 30, 2008 Author Share Posted August 30, 2008 ha thank's for the clarification! Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629577 Share on other sites More sharing options...
Zane Posted August 30, 2008 Share Posted August 30, 2008 you know PHP has a built in function that does all that already http://php.net/parse_url you might have to explode the args key when it runs and then use foreach like the above code does....but it would be WAY less lines Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629578 Share on other sites More sharing options...
ratcateme Posted August 30, 2008 Share Posted August 30, 2008 mean i will have to remember that Scott. Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629598 Share on other sites More sharing options...
Ken2k7 Posted August 30, 2008 Share Posted August 30, 2008 Or if you want variables instead of the array: <?php $data = substr($url, strpos($url, '?') + 1); $data = explode('&', $data); foreach ($data as $_value) { $_value = explode('=', $_value); ${$_value[0]} = $_value[1]; } echo $foo . " " . $test; ?> That script won't work if _value is a name for the GET field. (The one before the = sign.) Quote Link to comment https://forums.phpfreaks.com/topic/121961-extract-_get-variable/#findComment-629602 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.