PHP: Case-insensitive parameters

VeeBee picture VeeBee · Nov 18, 2010 · Viewed 12.4k times · Source

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?

Answer

BoltClock picture BoltClock · Nov 18, 2010

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.