Is there a function that works like $_GET ? I mean a function that transforms
"?var1=5&var2=true"
to
$var1=5;
$var2="true";
So that I can use one variable (string) in a function and fetch many variables from it?
Like:
function manual_GET($args){ /* ? */}
function myFunction($args)
{
manual_GET($args);
if(isset($var1))/* doesn't have to be this way, btw */
{
do_something($var1);
}
//etc
}
p.s. : I don't want to use $_GET with URL because this file is a class file (namely database_library.php) so I don't execute it directly, or make an AJAX call. I just require_once();
it.
Yes, there is. It is called parse_str
: http://php.net/manual/en/function.parse-str.php
One way to fix it.
function myFunction($args){
return parse_str($args,$values);
}
function parseQueryString($str) {
$op = array();
$pairs = explode("&", $str);
foreach ($pairs as $pair) {
list($k, $v) = array_map("urldecode", explode("=", $pair));
$op[$k] = $v;
}
return $op;
}
it works like parse_str
but doesn't convert spaces and dots to underscores
"?var1=5&var2=true"
foreach ($_GET AS $k=>$v){
$$k=$v;
}
echo $var1; // print 5
echo $var2; // print true