Last active
May 24, 2025 06:57
-
-
Save slackero/b955fa2d0fe1ec2c641eb224fa553fc7 to your computer and use it in GitHub Desktop.
Convert (unescape) all hex and octal chars into readable text
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 | |
/** | |
* Try to convert (unescape) all hex and octal chars into readable text | |
* '\x4e\145\166\x65\162\40\164\150\162\x6f\167\156' -> 'Never thrown' | |
*/ | |
$file = __DIR__ . '/my.file'; | |
$string = file_get_contents($file); | |
// convert octal part to readable text | |
$string = preg_replace_callback('/\\\\([0-7]{1,3})/', static function ($m) { | |
return mb_convert_encoding(chr(octdec($m[1])), 'UTF-8'); | |
}, $string); | |
// convert hexadecimal part to readable text | |
$string = preg_replace_callback('/\\\\x([0-9A-F]{1,2})/i', static function ($m) { | |
return mb_convert_encoding(chr(hexdec($m[1])), 'UTF-8'); | |
}, $string); | |
file_put_contents($file . '.converted', $string); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment