将Hex代码转换为PHP中的可读字符串

Moh*_*ril 4 php string hex

您好,StackOverflow社区,

这是我的问题,如何将具有十六进制代码的php转换为可读字符串?我的意思是所有内部这2个PHP代码..

<?php

echo "\x74\150\x69\163\x20\151\x73\40\x74\145\x73\164\x69\156\x67\40\x6f\156\x6c\171";

echo test['\x74\171\x70\145'];

echo range("\x61","\x7a");

?>
Run Code Online (Sandbox Code Playgroud)

这是不可读的代码,我需要一些PHP函数,可以将那些不可读的代码转换成可读的代码.所以转换后它会变成这样的..

<?php

echo "this is testing only";

echo test['type'];

echo range("a","z");

?>
Run Code Online (Sandbox Code Playgroud)

我知道我可以回应那个十六进制来改变它为可读字符串,但我有大量的php文件和很多的php文件,就像这样,所以我需要PHP函数,可以自动将它们全部转换为可读代码.

谢谢..

Sea*_*son 11

看起来您的代码不仅使用十六进制转义序列进行混淆,而且还使用八进制进行混淆.我写了这个函数来为你解码:

function decode_code($code){
    return preg_replace_callback(
        "@\\\(x)?([0-9a-f]{2,3})@",
        function($m){
            return chr($m[1]?hexdec($m[2]):octdec($m[2]));
        },
        $code
    );
}
Run Code Online (Sandbox Code Playgroud)

请在此处查看:http://codepad.viper-7.com/NjiL84

  • 使用@ \\\(x)?([0-9a-fA-F] {2,3})@用于大写的可比性 (3认同)

小智 7

我有混合的内容除了十六进制和oct之外还有正常的字符.

所以为了更新Sean的代码,我添加了以下内容

function decode_code($code)
{
    return preg_replace_callback('@\\\(x)?([0-9a-f]{2,3})@',
        function ($m) {
            if ($m[1]) {
                $hex = substr($m[2], 0, 2);
                $unhex = chr(hexdec($hex));
                if (strlen($m[2]) > 2) {
                    $unhex .= substr($m[2], 2);
                }
                return $unhex;
            } else {
                return chr(octdec($m[2]));
            }
        }, $code);
}
Run Code Online (Sandbox Code Playgroud)

示例字符串

"\152\163\x6f\x6e\137d\x65\143\157\x64e"
Run Code Online (Sandbox Code Playgroud)

解码输出

"json_decode"
Run Code Online (Sandbox Code Playgroud)