美文网首页
Nginx \x22 PHP和Ruby转换为原字符

Nginx \x22 PHP和Ruby转换为原字符

作者: 良先生爱生活 | 来源:发表于2020-04-24 16:53 被阅读0次

在Nginx日志中发现,nginx将特殊字符转化为16进制,看起来很不方便,使用PHP和Ruby转换为原字符,方法如下:

PHP

    public function x22()
    {
        $request_body = '{\x22no\x22:\x222020042309311\x22,\x22php\x22:\x22\x5Cu53d1\x5Cu53d1\x5Cu53d1\x22}';

        $new_request_body = '';

        $pt = 0;

        while ($pt < strlen($request_body)) {
            if ($request_body[$pt] == '\\' && $request_body[$pt + 1] == 'x') {

                //\x22为16进制,转换为10进制,再转化为ASCII码
                $new_request_body .= chr(hexdec($request_body[$pt] . $request_body[$pt + 1] . $request_body[$pt + 2] . $request_body[$pt + 3]));

                $pt = $pt + 4;

            } else {

                $new_request_body .= $request_body[$pt];

                $pt = $pt + 1;
            }
        }

        print_r($new_request_body);
    }
php.png

Ruby

#!/usr/bin/ruby -w
request_body = "{\x22no\x22:\x222020042309311\x22,\x22php\x22:\x22\x5Cu53d1\x5Cu53d1\x5Cu53d1\x22}"

new_request_body = ''
pt = 0
while pt < request_body.length do
    # 如果是中文, 转码
    if request_body[pt] == '\\' and request_body[pt + 1] == 'x' then
        word = (request_body[pt + 2] + request_body[pt + 3]).to_i(16).chr
        new_request_body = new_request_body + word
        pt = pt + 4
    # 如果是英文, 不处理
    else
        new_request_body = new_request_body + request_body[pt]
        pt = pt + 1
    end
end
puts '翻译结果:'
puts new_request_body
ruby.png

JavaScript

var request_body = '{\x22no\x22:\x222020042309311\x22,\x22php\x22:\x22\x5Cu53d1\x5Cu53d1\x5Cu53d1\x22}';

var new_request_body = '';

var pt = 0;

while(pt < request_body.length){

    if (request_body[pt] == '\\' && request_body[pt + 1] == 'x') {

        //\x22为16进制,转换为10进制,再转化为ASCII码
        new_request_body += parseInt(request_body[pt] + request_body[pt + 1] + request_body[pt + 2] + request_body[pt + 3],16).fromCharCode();

        pt = pt + 4;

    } else {

        new_request_body += request_body[pt];

        pt = pt + 1;
    }
}

console.log(new_request_body);

写作不易,如果您觉得对您有帮助,请您给我点个赞或者关注我 [送你小花花~]

相关文章

网友评论

      本文标题:Nginx \x22 PHP和Ruby转换为原字符

      本文链接:https://www.haomeiwen.com/subject/ennfwhtx.html