2016-04-22 45 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

相關問題