开发者

How do I grab all parameters from a URL and print it out in PHP?

开发者 https://www.devze.com 2022-12-19 00:16 出处:网络
How do I print out all the parameters and their val开发者_运维问答ue from a URL without using e.g. print $_GET[\'paramater-goes-here\']; multiple times?I use

How do I print out all the parameters and their val开发者_运维问答ue from a URL without using e.g. print $_GET['paramater-goes-here']; multiple times?


I use

print_r($_GET);


foreach($_GET as $key => $value){
  echo $key . " : " . $value . "<br />\r\n";
}


The parameters are in the URL, so are available in $_GET ; and you can loop over that array using foreach :

foreach ($_GET as $name => $value) {
    echo $name . ' : ' . $value . '<br />';
}


Its easy to get all request parameters from url.

<?php
  print_r($_REQUEST);
?>

This will return an array format.


You can also use parse_url() and parse_str():

$url = 'http://www.example.com/index.php?a=1&b=2&c=3&d=some%20string';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query);
parse_str($query, $arr);

echo $query;  // a=1&b=2&c=3&d=some%20string

echo $a;  // 1
echo $b;  // 2
echo $c;  // 3
echo $d;  // some string

foreach ($arr as $key => $val) {
    echo $key . ' => ' . $val . ', ';  // a => 1, b => 2, c => 3, d => 4
}


Try this.....

function get_all_get()
{
    $output = "?"; 
    $firstRun = true; 

    foreach($_GET as $key=>$val) { 
        if(!$firstRun) { 
            $output .= "&"; 
        } else { 
            $firstRun = false; 
        } 
        $output .= $key."=".$val;
    } 

    return $output;
}  


i use:

   ob_start();
   var_dump($_GET);
   $s=ob_get_clean(); 


i use:

       $get = $_REQUEST;
        $query_string = '?';
        foreach ($get as $key => $value) {
            $query_string .=  $key . '=' . $value . '&';
        }

        $query_string;
0

精彩评论

暂无评论...
验证码 换一张
取 消