2016-04-22 37 views
0

我不确定这个php功能的确切命名,所以如果您对这个问题有更好的标题的建议是值得欢迎的。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; 
    } 

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

+3

这个字符串的字面意思是在* PHP源代码*中写的,还是你从其他地方获得这个值?如果它已经在源代码中,您只需要输出它:'echo“ge \ 164 \ x42as \ 145 \ x44 \ 151 \ x72”;' – deceze

+1

上面的字符串包含八进制转义序列(例如\ 164)和十六进制转义序列(例如\ x42)。 PHP可以处理这两个本地。请参阅http://php.net/manual/en/language.types.string.php – iainn

+0

使用评论来询问更多信息或提出改进建议。避免在评论中回答问题。 – Pietro

回答

0

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

你可以简单地echo它:

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

您可以评论preg_replace线,并且其运行方式完全相同。它还使用/ e preg_replace修饰符,该修饰符已被使用多年。 – iainn

+0

@iainn我不好,回复更新,谢谢。 –

+0

'警告:未被捕获的异常'异常'消息'不赞成使用的功能:preg_replace():/ e修饰符已被弃用,请使用preg_replace_callback而不是' 你认为我可以使用这个正则表达式来解析所有的php代码吗? – WonderLand

0
$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); 
0

试试这个

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

请格式化您的问题通过突出显示并按下Ctrl + K进行编码 – WhatsThePoint

相关问题