2009-06-21 32 views
1

有什麼辦法可以在PHP中享受decodeValue()函數嗎?我將這些encodedValue值發佈到PHP文件中,我需要在PHP中以數組的形式使用它們。ExtJS:DecodeValue in PHP

我該如何結束一個PHP數組或從Ext編碼狀態的東西?或者,有沒有其他方法可以使編碼的值能夠在PHP中輕鬆讀取?以下是功能代碼:

decodeValue : function(cookie){ 
     var re = /^(a|n|d|b|s|o)\:(.*)$/; 
     var matches = re.exec(unescape(cookie)); 
     if(!matches || !matches[1]) return; // non state cookie 
     var type = matches[1]; 
     var v = matches[2]; 
     switch(type){ 
      case "n": 
       return parseFloat(v); 
      case "d": 
       return new Date(Date.parse(v)); 
      case "b": 
       return (v == "1"); 
      case "a": 
       var all = []; 
       var values = v.split("^"); 
       for(var i = 0, len = values.length; i < len; i++){ 
        all.push(this.decodeValue(values[i])); 
       } 
       return all; 
      case "o": 
       var all = {}; 
       var values = v.split("^"); 
       for(var i = 0, len = values.length; i < len; i++){ 
        var kv = values[i].split("="); 
        all[kv[0]] = this.decodeValue(kv[1]); 
       } 
       return all; 
      default: 
       return v; 
     } 
    } 

謝謝。

回答

1

下面是我的PHP端口。我使用DateTime類代替Date,因爲它是最接近的PHP等價物,但您也可以使用strftime()來獲得Unix時間戳或您喜歡的任何方法。另外,對於類型'o',我返回的是一個數組而不是一個對象,由對象的參數名稱作爲鍵。

下面的代碼:

function decodeValue($cookie) { 
    $cookie = urldecode($cookie); 
    $re = '/^(a|n|d|b|s|o)\:(.*)$/'; 
    $matches = array(); 
    preg_match($re, $cookie, $matches); 
    if(!$matches || !$matches[1]) return; // non state cookie 
    $type = $matches[1]; 
    $v = $matches[2]; 
    switch ($type){ 
     case "n": 
      return floatval($v); 
     case "d": 
      return new DateTime($v); 
     case "b": 
      return ($v == "1" ? true : false); 
     case "a": 
      $all = array(); 
      $values = explode('^', $v); 
      $len = count($values); 
      for ($i = 0; $i < $len; $i++) { 
       $all.push(decodeValue($values[$i])); 
      } 
      return $all; 
     case "o": 
      $all = array(); 
      $values = explode('^', $v); 
      $len = count($values); 
      for($i = 0; $i < $len; $i++){ 
       $kv = explode('=', $values[$i]); 
       $all[$kv[0]] = decodeValue($kv[1]); 
      } 
      return $all; 
     default: 
      return $v; 
    } 
} 
1

修正了在代碼中的錯誤。現在二級/三級數組應該可以正常工作。

function decodeValue($cookie) { 
    $cookie = urldecode($cookie); 
    $re = '/^(a|n|d|b|s|o)\:(.*)$/'; 
    $matches = array(); 
    preg_match($re, $cookie, $matches); 
    if(!$matches || !$matches[1]) return $cookie; // non state cookie 
    $type = $matches[1]; 
    $v = $matches[2]; 

    switch ($type){ 
     case "n": 
      return floatval($v); 
     case "d": 
      return new DateTime($v); 
     case "b": 
      return ($v == "1" ? true : false); 
     case "a": 
      $all = array(); 
      $values = explode('^', $v); 
      $len = count($values); 
      for ($i = 0; $i < $len; $i++) { 
       $all.array_push(decodeValue($values[$i])); 
      } 
      return $all; 
     case "o": 
      $all = array(); 
      $values = explode('^', $v); 
      $len = count($values); 
      for($i = 0; $i < $len; $i++){ 
       $kv = explode('=', $values[$i],2); 
       if(count($kv)==1){ 
        $all[] = decodeValue($kv[0]); 
       }else{ 
        $all[$kv[0]] = decodeValue($kv[1]); 
       } 
      } 
      return $all; 
     default: 
      return $v; 
    } 
} 
1
$all.array_push(decodeValue($values[$i])); 

需要與

$all[] = decodeValue($values[$i]); 
被替換