开发者

What are the precise rules/PHP function for encoding strings into POST arrays?

Just getting into PHP web development. I've got an HTML form where a user checks some series of dynamically-generated checkboxes, and submits via POST. On the PHP side, I want to check which of the check-boxes were clicked.

I have an array $full_list, and am doing something like

$selected_checkboxes = array_filter($full_list, function($item) {
    array_key_exists($item, $_POST);
  }

I run into problems when a list item is named, for example "Peanut Butter", since in the POST array it is named "Peanut_Butter".

I could cert开发者_开发百科ainly just str_replace " " with "_" before checking array_key_exists, but I imagine that there is a more fundamental encoding problem here; specifically, I'm not sure of exactly what layer transforms normal strings in HTML Forms (value="Peanut Butter") into "Peanut_Butter".

So:

  • what layer is responsible for this conversion? Is it the browser?
  • what are the exact conversion rules, and is there a PHP function out there that will replicate that exact conversion?


The accepted answer by Byron Whitlock is wrong. PHP is indeed the culprit here. See the PHP manual:

Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].

Also refer to the answers of this similar question.


PHP doesn't do this. There is something on the client side that is converting spaces to underscores.

The browser should encode each variable using the equivalent of urlencode(). PHP will automatically decode these strings so it is transparent for the programmer.

edit The equivalent in javaScript is escape(). But it is very very likely there is some js code manually converting spaces to underscores.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜