• 一个压缩算法 可以适当的用来编码


    function encode($string) {
        $dict = array_flip(range("", "xFF"));
        $dict_size = 256;
        $word = $string[0];
    
        $dict_count = 256;
        $bits = 8; 
        $bits_max = 256;
        $return = "";
        $rest = 0;
        $rest_length = 0;
    
        for ($i = 1, $j = strlen($string); $i < $j; $i++) {
            $x = $string[$i];
            $y = $word . $x;
            if (isset($dict[$y])) {
                $word .= $x;
            } else {
                $rest = ($rest << $bits) + $dict[$word];
                $rest_length += $bits;
                $dict_count++;
                if ($dict_count > $bits_max) {
                    $bits_max = 1 << ++$bits;
                }
                while ($rest_length > 7) {
                    $rest_length -= 8;
                    $return .= chr($rest >> $rest_length);
                    $rest &= (1 << $rest_length) - 1;
                }
                $dict[$y] = $dict_size++;
                $word = $x;
            }
        }
    
        $rest = ($rest << $bits) + $dict[$word];
        $rest_length += $bits;
        $dict_count++;
        if ($dict_count > $bits_max) {
            $bits_max = 1 << ++$bits;
        }
        while ($rest_length > 0) {
            if($rest_length>7){
                $rest_length -= 8;
                $return .= chr($rest >> $rest_length);
                $rest &= (1 << $rest_length) - 1;
            }else{
                $return .= chr($rest << (8 - $rest_length));
                $rest_length = 0;
            }
        }
    
        return $return;
    }
    
    
    function decode($binary) {
        $rest = 0;
        $rest_length = 0;
        $out_count = 257;
        $bits = 9;
        $bits_max = 512;
        $dict = range("", "xFF");
        $w = $binary[0];
        $return = $w;
        
        for ($i = 1, $j = strlen($binary); $i < $j; $i++) {
            $rest = ($rest << 8) + ord($binary[$i]);
            $rest_length += 8;
            if ($rest_length >= $bits) {
                $rest_length -= $bits;
                $e = $dict[$rest >> $rest_length];
                if (!isset($e)) {
                    $e = $w . $w[0];
                }
                $return .= $e;
                $dict[] = $w . $e[0];
                $w = $e;          
                $rest &= (1 << $rest_length) - 1;
                if (++$out_count > $bits_max) {
                    $bits_max = 1 << ++$bits;
                }
            }
        }
        
        return $return;
    }
    
    
    
    echo decode(encode(file_get_contents('http://www.baidu.com')));
  • 相关阅读:
    前端-html/css
    数据结构-python
    接口测试-并发处理
    接口测试-高级运用
    接口测试-模拟网络请求
    接口测试-基础
    Jenkins-基础
    appium安装及环境搭建、入门
    Week12-unittest单元测试
    Redis在windows下安装与配置
  • 原文地址:https://www.cnblogs.com/freespider/p/3462954.html
Copyright © 2020-2023  润新知