PHP - 转换八进制/十六进制转义序列


PHP - Convert octal/hexadecimal escape sequences

我不确定这个php功能的确切命名,所以如果你对这个问题有更好的标题的建议,欢迎。

关键是,我有一些以这种方式编写的字符串"ge'164'x42as'145'x44'151'x72"我想将它们转换为可读字符(例如上面的字符串值是"getBaseDir"

如何使用 php 以编程方式完成此操作?

更新
这些字符串包含在 php 源文件中,我想解析和清理该文件以提高可读性。

因此,我将不胜感激一个解决方案,它为我提供了一种一次解析和隐藏此字符串的方法(例如使用正则表达式)......

这里是代码的一部分,以便更容易理解场景

 public function cmp($x74, $x7a)
    {
        $x76 = $this->x1c->x3380->{$this->xc1->x3380->xe269};
        $x12213 = "'x68'145'x6c'x70'x65'x72";
        $x11f45 = "'x67'x65'164'123t'x6fr'x65Co'156'146'x69'147";
        if ($x76(${$this->x83->x3380->{$this->x83->x3380->{$this->x83->x3380->xd341}}}) == $x76(${$this->x83->x336e->{$this->xc1->x336e->{$this->xc1->x336e->x8445}}})) {
            return 0;
        }
        return ($x76(${$this->x83->x334c->{$this->x83->x334c->x3423}}) < $x76(${$this->x83->x336e->{$this->xc1->x336e->{$this->xc1->x336e->x8445}}})) ? 1 : -1;
    }

只是为了清除上面的代码是我们合法购买的扩展的一部分,但我们需要自定义。

$string = '"'125n'141'x62'154'145'40to'x67'145'156e'x72'141t'145'x20'x74'x68e'40d'x61t'141'40'146'145'145d'x2e"';
'' convert the octal into string
$string = preg_replace_callback('/''''([0-7]{1,3})/', function ($m) {
    return chr(octdec($m[1]));
}, $string);
'' convert the hexadecimal part of the string
$string = preg_replace_callback('/''''x([0-9A-F]{1,2})/i', function ($m) {
    return chr(hexdec($m[1]));
}, $string);

在这种特殊情况下,我需要解析与""分隔的所有字符串匹配的完整文件内容并转换它们,这里是完整的解决方案

$content = file_get_contents($filepath);
// match all string delimited by ""
$content = preg_replace_callback("/('".*?'")/s ", function ($m) {
    $string = $m[1];
    '' convert the octal into string
    $string = preg_replace_callback('/''''([0-7]{1,3})/', function ($m) {
        return chr(octdec($m[1]));
    }, $string);
    '' convert the hexadecimal part of the string
    $string = preg_replace_callback('/''''x([0-9A-F]{1,2})/i', function ($m) {
        return chr(hexdec($m[1]));
    }, $string);
    return $string;
}, $content);

如何使用 php 以编程方式完成此操作?

你可以简单地回显它:

echo "ge'164'x42as'145'x44'151'x72";
//getBaseDir

试试这个

$ret = print_r("''012", true);