0

How can i dynamically received the parameter with dot in php.

Say i know there is a URL parameter "hl" but the there is some dynamic values are appended with in the parameter name like

"h1.u", "hl.p" is it possible to these parameters with dynamic names?

Kathick
  • 1,395
  • 5
  • 19
  • 30

4 Answers4

2

It appears that this is known behavior and there are some workarounds out there (creating a function to get the actual $_GET parameters).

Check out this question for more details: Get PHP to stop replacing '.' characters in $_GET or $_POST arrays?

Community
  • 1
  • 1
Jim D
  • 988
  • 10
  • 18
0

$_GET is an enumerated array.

That means, you can loop through the entire $_GET array and check for 'variants'.

Meaning the parameter h1.u would be located in $_GET['h1.u'];

Searching could work like this:

foreach($_GET as $key => $value) {
    if(strpos($key, "h1") !== false) {
        //Do stuff
    }
}

This code will iterate through the entire array and look for anything which contains 'h1' somewhere in it's key and do something with it.

strpos will return boolean false if it's not found, which is why you need to do a typecheck instead of checking for not 0, since h1 could well be located at position 0 (first character)

Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24
  • Refugnic, am I missing something? Far as I know, dots aren't allowable in GET variable names. When you run a php script with GET vars like ?h1.u=14&h1.p=7, it will be translated into $_GET['h1_u'] and $_GET['h1_p']. – Topher Hunt Jun 08 '13 at 12:45
  • @TopherHunt Would you mind reading the comment I posted in the reply with the 2 upvotes? I didn't know there was a problem with those variable names, but my answer was generic enough to remain standing. If you want to know if the key name has a particular chain of characters in it, you can do it like that. – Refugnic Eternium Jun 08 '13 at 17:08
0
$gets=array_keys($_GET);
foreach($item in $gets){
  $check1=explode($item, ".");
  if(count($check1) <2){
    //No appended keys recieved
  }

  else{
    $key = $check1[0];
    $apeendedPart= $check1[1];
  }

 //Do something with these two
}

Not tested, small bugs maybe present.. I'll correct it if you found one.

Ronnie
  • 512
  • 5
  • 12
0

As far as I know, dot characters aren't allowable in GET variables. GET replaces them with '_'. This is fine if your variables won't naturally have any underscores: in the beginning of your PHP script, you just look for any dynamic combination of 'h1_' plus the whole deck of possible letters (or, adapt Ronnie's answer to use '_' instead of '.').

I run into situations where _ is a valid character in GET variable names, but I need a way to specify '.' too. So I use Javascript to ensure that all '.' are turned into '__' (two underscores) beforehand, then I look for this pattern in the receiving PHP.

Topher Hunt
  • 4,404
  • 2
  • 27
  • 51