Jump to content

extract $_GET variable


bftwofreak

Recommended Posts

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.

$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.

 

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.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.