1

decodeValue()PHPでも関数を楽しむ方法はありますか?これらのencodedValue値をPHPファイルに投稿しています.PHPでそれらを配列として扱う必要があります。

Ext でエンコードされた状態から PHP 配列または何かを取得するにはどうすればよいですか? または、エンコードされた値を 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;
        }
    }

ありがとうございました。

4

3 に答える 3

1

以下は私のPHPへの移植です。Date の代わりに 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;
    }
}
于 2009-06-21T13:58:08.283 に答える
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;
    }
}
于 2011-11-16T20:19:18.510 に答える