base64 encode와 serialize를 한번에

배열형태의 리스트를 json말고 base64encode와 serialize로 넣고싶을때 사용하시면됩니다.

넣을때 wv_base64_encode_serialize($data)로 넣고, 풀때 $arr= wv_base64_decode_unserialize($data)로 쓰시면됩니다. 아래 코드는 common.lib.php에 넣어주세요

[code]

if(!function_exists('wv_is_base64_encoded')){
    function wv_is_base64_encoded($data){
        return base64_encode(base64_decode($data, true)) === $data;
    }
}
if(!function_exists('wv_is_serialized')){
    function wv_is_serialized( $data, $strict = true ) {
        // If it isn't a string, it isn't serialized.
        if ( ! is_string( $data ) ) {
            return false;
        }
        $data = trim( $data );
        if ( 'N;' === $data ) {
            return true;
        }
        if ( strlen( $data ) < 4 ) {
            return false;
        }
        if ( ':' !== $data[1] ) {
            return false;
        }
        if ( $strict ) {
            $lastc = substr( $data, -1 );
            if ( ';' !== $lastc && '}' !== $lastc ) {
                return false;
            }
        } else {
            $semicolon = strpos( $data, ';' );
            $brace     = strpos( $data, '}' );
            // Either ; or } must exist.
            if ( false === $semicolon && false === $brace ) {
                return false;
            }
            // But neither must be in the first X characters.
            if ( false !== $semicolon && $semicolon < 3 ) {
                return false;
            }
            if ( false !== $brace && $brace < 4 ) {
                return false;
            }
        }
        $token = $data[0];
        switch ( $token ) {
            case 's':
                if ( $strict ) {
                    if ( '"' !== substr( $data, -2, 1 ) ) {
                        return false;
                    }
                } elseif ( false === strpos( $data, '"' ) ) {
                    return false;
                }
            // Or else fall through.
            case 'a':
            case 'O':
            case 'E':
                return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
            case 'b':
            case 'i':
            case 'd':
                $end = $strict ? '$' : '';
                return (bool) preg_match( "/^{$token}:[0-9.E+-]+;$end/", $data );
        }
        return false;
    }
}
if(!function_exists('wv_base64_decode_unserialize')){
    function wv_base64_decode_unserialize($data){

        if(wv_is_base64_encoded($data)){
            $data = base64_decode($data);
        }

        if(wv_is_serialized($data)){
            $data = unserialize($data);
        }

        return $data;
    }
}
if(!function_exists('wv_base64_encode_serialize')){
    function wv_base64_encode_serialize($data){



        if(!wv_is_serialized($data)){
            $data = serialize($data);
        }

        if(!wv_is_base64_encoded($data)){
            $data = base64_encode($data);
        }

        return $data;
    }
}

[/code]

|

댓글 2개

감사합니다 ^^

댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

+
제목 글쓴이 날짜 조회
6개월 전 조회 778
6개월 전 조회 681
6개월 전 조회 915
6개월 전 조회 1,271
7개월 전 조회 811
7개월 전 조회 795
7개월 전 조회 881
7개월 전 조회 1,012
7개월 전 조회 677
7개월 전 조회 735
7개월 전 조회 1,539
7개월 전 조회 846
7개월 전 조회 670
7개월 전 조회 885
7개월 전 조회 630
7개월 전 조회 680
8개월 전 조회 530
8개월 전 조회 666
8개월 전 조회 710
8개월 전 조회 843
8개월 전 조회 725
8개월 전 조회 784
8개월 전 조회 779
8개월 전 조회 720
8개월 전 조회 594
8개월 전 조회 573
8개월 전 조회 625
8개월 전 조회 1,588
8개월 전 조회 712
8개월 전 조회 635