سؤال

هل هناك أي وسيلة أن أتمكن من الاستمتاع وظيفة decodeValue() في PHP، أيضا؟ وأنا على نشر تلك القيم encodedValue إلى ملف PHP ولست بحاجة للعمل معهم في PHP كما صفيف.

وكيف يمكنني ينتهي مع مجموعة 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;
        }
    }

وشكرا لكم.

هل كانت مفيدة؟

المحلول

وفيما يلي ميناء جهدي لPHP. لقد استخدمت الطبقة التاريخ والوقت بدلا من التسجيل لأنه هو الأقرب PHP ما يعادلها، ولكن هل يمكن أيضا استخدام STRFTIME () للحصول على طابع زمني يونيكس، أو أيا كانت الطريقة التي تفضلها. أيضا، لنوع 'س' أعود مجموعة بدلا من كائن، مرتبطا بأسماء المعلمة الكائن.

وهنا رمز:

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;
    }
}

نصائح أخرى

وإصلاح الخلل في التعليمات البرمجية. الآن يجب أن تعمل مجموعة المستوى الثاني / المستوى الثالث بشكل صحيح.

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;
    }
}
$all.array_push(decodeValue($values[$i]));

وتحتاج إلى استبدال مع

$all[] = decodeValue($values[$i]);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top