2009-06-21 33 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]); 
被替换