This will do:
$string = '\300'; $string = preg_replace_callback('/\\\\\d{1,3}/', function (array $match) { return pack('C', octdec($match[0])); }, $string);
It matches any backslash sequence followed by up to three numbers, and converts that number from an octal number to a binary string. Which has the same result as "\300" .
Note that this will not work the same for shielded screens; that is, "\\300" will result in the literal \300 , while the above code converts it.
If you want all possible double-quoted string rules to be respected without re-evaluating them manually, it is best to simply eval("return \"$string\"") , but this also has a few caveats.
source share