【问题标题】:PHP - Convert octal/hexadecimal escape sequencesPHP - 转换八进制/十六进制转义序列
【发布时间】:2016-08-15 20:48:57
【问题描述】:

我不确定这个 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;
    }

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

【问题讨论】:

  • 该字符串是在 PHP 源代码 中按字面意思编写的,还是您从其他地方获得的值?如果它已经在源代码中,你只需要输出它:echo "ge\164\x42as\145\x44\151\x72";
  • 上面的字符串包含八进制转义序列(例如 \164)和十六进制转义序列(例如 \x42)的混合。 PHP 可以本机处理这两种情况。见php.net/manual/en/language.types.string.php
  • 使用 cmets 询问更多信息或提出改进建议。避免在 cmets 中回答问题。
  • @deceze 是的,它在代码中(实际上这是一些开发人员使用scrumbled 他们的代码的愚蠢方式,我讨厌这种事情)我会测试你的建议......如果你可以把它放在答案中,一旦我确定它对我有用,我会接受它
  • 所以echo "ge\164\x42as\145\x44\151\x72"; 工作...!?

标签: php string ascii octal


【解决方案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);

【讨论】:

    【解决方案2】:

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

    你可以简单地回显它:

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

    【讨论】:

    • 您可以注释掉 preg_replace 行,它的运行方式完全相同。它还使用已弃用多年的 /e preg_replace 修饰符。
    • Warning: Uncaught exception 'Exception' with message 'Deprecated functionality: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead 你认为我可以使用这个正则表达式来一次解析所有 php 代码吗?
    • 其实echo是不够的,因为我需要一个返回值
    • @WonderLand 答案已更新,当您发表评论时,无论如何。
    • @WonderLand 你必须弄清楚你是在什么情况下得到这些字符串的。同样,仅将此字符串编写为 PHP 源代码文字的事实“反混淆”了它。从那里你可以将它分配给一个变量或输出它或做任何你想做的事情。如果这不是您的起始条件,请澄清您的情况和期望的结果。
    【解决方案3】:

    试试这个

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

    【讨论】:

    • 您能否通过突出显示代码并按 Ctrl+K 来格式化您的代码
    猜你喜欢
    • 2011-08-21
    • 2015-12-12
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 2013-03-29
    • 2021-01-29
    相关资源
    最近更新 更多