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?
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.
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.
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().
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With