Last active
October 31, 2022 04:54
-
-
Save Otto42/fcc3ddd3ff101246093657191750d4f8 to your computer and use it in GitHub Desktop.
Decode a file with encoded strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Decodes files that have a bunch of strings with things like "\x6d" and "\155" and such in them. | |
// Meat of the code from https://stackoverflow.com/questions/13774215/convert-hex-code-into-readable-string-in-php | |
// run me like so on command line: | |
// php decode.php < encoded.php > output.php | |
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); | |
} | |
$file = file_get_contents('php://stdin'); | |
echo decode_code($file); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment