开发者

PHP: Case-insensitive parameters

开发者 https://www.devze.com 2023-01-26 04:24 出处:网络
How do I accept passed GET or POST values case insensitively?开发者_StackOverflow Like sample.php?OrderBy=asc will still be the same as sample.php?orderby=asc or sample.php?ORDERBY=asc

How do I accept passed GET or POST values case insensitively?开发者_StackOverflow

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消