Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Case-insensitive parameters

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?

like image 217
VeeBee Avatar asked Nov 18 '10 03:11

VeeBee


People also ask

Are PHP cases insensitive?

In PHP, class names as well as function/method names are case-insensitive, but it is considered good practice to them functions as they appear in their declaration. In the following example, the function that is defined as exampleFunction() is called as ExampleFunction() , which is discouraged.

How do you make a function parameter in case-insensitive?

The most basic way to do case insensitive string comparison in JavaScript is using either the toLowerCase() or toUpperCase() method to make sure both strings are either all lowercase or all uppercase.

How do you match case-sensitive in PHP?

Definition and Usage Tip: The strcasecmp() function is binary-safe and case-insensitive. Tip: This function is similar to the strncasecmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncasecmp().


1 Answers

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.

like image 97
BoltClock Avatar answered Sep 21 '22 00:09

BoltClock