29

How do I accept passed GET or POST values case insensitively?

Like sample.php?OrderBy=asc will still be the same as sample.php?orderby=asc or sample.php?ORDERBY=asc

Is there a means of achieving the above efficiently?

3
  • Not sure of a good solution, but I ran into a problem like that where the hashed password has gotten lower-cased and was unable to use it; so be careful, and I would suggest looking to see why this happens in the first place; usually when converting from HTTP to HTTPS (or back), the URL gets lower-cased.
    – BeemerGuy
    Nov 18, 2010 at 3:32
  • 2
    What is the reason to change case?
    – zerkms
    Nov 18, 2010 at 3:32
  • 3
    well im working on an API where the system previously serves other API clients that use uppercase and recently we made API some changes including the decision to honor lower-cased parameters. so in order to maintain backward compatibility with the previous API users, i am faced with this kind of problem.
    – VeeBee
    Nov 18, 2010 at 3:40

1 Answer 1

58

You could use array_change_key_case() to create a copy of $_GET with either all uppercase or all lowercase keys.

$_GET_lower = array_change_key_case($_GET, CASE_LOWER);
$orderby = isset($_GET_lower['orderby']) ? $_GET_lower['orderby'] : 'asc';
echo $orderby;

(I say "create a copy" simply because I don't like polluting the original superglobals, but it's your choice to overwrite them if you wish.)

For that matter, it'd still be better if you just stick to case-sensitive matching since it might be easier on both search engine and human eyes, as well as easier on your code... EDIT: OK, based on your comment I can see why you'd want to do something like this.

3
  • What is the reason to change case?
    – zerkms
    Nov 18, 2010 at 3:32
  • 1
    When launching the default web browser with a Shell command from VBA, I've had the url mysteriously changed to all lower case if the browser is the Windows 10 Edge browser. But not on all Windows 10 computers, only some. Jan 23, 2018 at 20:25
  • I have been working on an API that needed to recognize that ID, Id and id are the same... Thanks @BoltClock
    – RealSollyM
    Apr 20, 2018 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.