我的 HTML 复选框表单字段的名称如下所示:
name = "Some File name.pdf"
PHP POST 数组看起来像这样(一些字符被下划线替换):
array(
"Some_File_name_pdf" => "on"
)
是否有一个 PHP 函数可以用来将文件名字符串转换为与 POST 数组中显示的完全相同的文件名字符串?我对 str_replace 不感兴趣。 我希望能够做到这一点:
$myfilename = $obj->getFileName(); // returns "Some File name.pdf"
$result = isset($_POST[some_encoding_function($myfilename)]);
some_encoding_function 应采用“Some File name.pdf”之类的字符串并返回“Some_File_name_pdf”之类的内容;
最佳答案
Dots and spaces in variable names are converted to underscores.
这只需要简单的字符串替换。
然而,根据a comment on that page :
The full list of field-name characters that PHP converts to _ (underscore) is the following (not just dot):
chr(32) ( ) (space)
chr(46) (.) (dot)
chr(91) ([) (open square bracket)
chr(128) - chr(159) (various)
如果该评论是正确的,那么您应该可以使用类似的函数
function underscorify($s)
{
return preg_replace('/[ \.\[\x80-\x9F]/', '_', $s);
}
但请注意,chr(128) - chr(159)
是不明确的,因为没有提到这是否依赖于字符编码。
它可能指的是 €
中的所有 ASCII 字符。至Ÿ
,它可能引用 \u0080
中的所有 UTF-8 控制字符-\u009F
,或者可能只是进行硬编码以检查 b >= 128 && b <= 159
的字节值.
关于php - 对字符串进行编码以匹配 PHP POST 数组中编码的表单字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30838852/