编码字符串以匹配PHP POST数组中的编码表单字段名称 [英] Encode string to match encoded form field name in PHP POST array

查看:153
本文介绍了编码字符串以匹配PHP POST数组中的编码表单字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML复选框表单字段的名称是这样的:

  name =Some File name.pdf

PHP POST数组看起来像这样(一些字符被下划线替换):

 数组(
Some_File_name_pdf=&on

/ pre>

有没有PHP函数可以用来完全按照POST数组中显示的文件名字符串进行转换?我对str_replace不感兴趣
我想要这样做:

  $ myfilename = $ obj-> getFileName(); //返回Some File name.pdf
$ result = isset($ _ POST [some_encoding_function($ myfilename)]);

some_encoding_function应该使用一些字符串,如Some File name.pdf,并返回Some_File_name_pdf ;

解决方案

根据 PHP文档


变量名中的点和空格转换为下划线。


这将只需要一个简单的字符串替换。

但是,根据该页面上的评论


PHP转换为_(下划线)的字段名字符的完整列表如下(不只是点):

chr(32)() (空格)

chr(46)(。)(dot)

chr(91)([)(开方括号)

chr(128) - chr(159)(各种)


如果该评论是正确的,那么你应该很好,像

 函数underscorify($ s)
{
return preg_replace('/ [\.\ [\x80-\x9F] /','_',$ s);
}

请注意, chr(128) - chr (159)是不明确的,因为没有提到这是否符合字符编码。

它可以引用中的所有ASCII字符€Ÿ,可以参考来自 \\\€ - \\\Ÿ ,或者可以简单地硬编码来检查 b> = 128&&& b <= 159


The name of my HTML checkbox form field is something like this:

name = "Some File name.pdf"

The PHP POST array looks something like this (some characters replaced by underscore):

array(
 "Some_File_name_pdf" => "on"
)

Is there a PHP function I can use to convert a filename string exactly as it appears in the POST array? I am not interested in str_replace. I want to be able to do this:

$myfilename = $obj->getFileName(); // returns "Some File name.pdf"
$result = isset($_POST[some_encoding_function($myfilename)]);

The some_encoding_function should take a string like "Some File name.pdf" and return something like "Some_File_name_pdf";

解决方案

According to the PHP documentation:

Dots and spaces in variable names are converted to underscores.

This would require only a trivial string substitution.
However, according to 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)

If that comment is correct, then you should be good to go with a function like

function underscorify($s)
{
    return preg_replace('/[ \.\[\x80-\x9F]/', '_', $s);
}

Note however, that chr(128) - chr(159) is ambiguous, as it is not mentioned whether this is character-encoding-dependent or not.
It may refer to all ASCII characters from to Ÿ, it may refer to all UTF-8 control characters from \u0080-\u009F, or it may simply be hardcoded to check the byte value for b >= 128 && b <= 159.

这篇关于编码字符串以匹配PHP POST数组中的编码表单字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆