Convert Hex Code into readable string in PHP

后端 未结 4 514
[愿得一人]
[愿得一人] 2020-12-11 06:26

Hi StackOverflow Community,

Here is my question, how to convert php that has hex code into readable string ? On what I mean is all inside this 2 php code..



        
4条回答
  •  伪装坚强ぢ
    2020-12-11 06:53

    I had mixed content where besides hex and oct there was also normal chars.

    So to update Sean's code above I added following

    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);
    }
    

    Example string

    "\152\163\x6f\x6e\137d\x65\143\157\x64e"
    

    Decoded output

    "json_decode"
    

提交回复
热议问题