我不确定这个 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);
试试这个
<?php
$inputString = "ge\164\x42as\145\x44\151\x72";
// Replace escape sequences with their corresponding characters
$properFormatString = preg_replace_callback('/\\\\([0-7]{1,3})/', function ($matches) {
return chr(octdec($matches[1]));
}, $inputString);
echo $properFormatString;
?>