2009-06-21 4 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クラスを使用しましたが、これは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

は、コード内のバグを修正しました。今度は第2レベル/第3レベル配列が正しく動作するはずです。

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]); 
と交換する必要があります