Skip to content

Instantly share code, notes, and snippets.

@slackero
Last active May 24, 2025 06:57
Show Gist options
  • Save slackero/b955fa2d0fe1ec2c641eb224fa553fc7 to your computer and use it in GitHub Desktop.
Save slackero/b955fa2d0fe1ec2c641eb224fa553fc7 to your computer and use it in GitHub Desktop.
Convert (unescape) all hex and octal chars into readable text
<?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