문제

내가 즐길 수있는 방법이 있습니까? 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;
        }
    }

고맙습니다.

도움이 되었습니까?

해결책

아래는 내 포트입니다. 가장 가까운 PHP 대신 대신 DateTime 클래스를 사용했지만 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;
    }
}

다른 팁

코드의 버그를 수정했습니다. 이제 두 번째 레벨/세 번째 레벨 배열이 올바르게 작동해야합니다.

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