Skip to content

Instantly share code, notes, and snippets.

@Otto42
Last active October 31, 2022 04:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Otto42/fcc3ddd3ff101246093657191750d4f8 to your computer and use it in GitHub Desktop.
Save Otto42/fcc3ddd3ff101246093657191750d4f8 to your computer and use it in GitHub Desktop.
Decode a file with encoded strings
<?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