Location: PHPKode > scripts > ArZipStr > arzipstr/GrZipStr.class.php
<?php
// ----------------------------------------------------------------------
// Copyright (C) 2007 by Khaled Al-Shamaa.
// http://www.al-shamaa.com/php/arabic
// ----------------------------------------------------------------------
// LICENSE

// This program is open source product; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Class Name: Zip German String
// Filename:   GrZipStr.class.php
// Original    Author(s): Khaled Al-Sham'aa <hide@address.com>
// Purpose:    This class will compress given German string in binary format
//             using information on frequency analysis in German language
//             German string should be encoded using ISO-8859-1 charset
//             Avarage compress string will be 70% length of origenal.
// ----------------------------------------------------------------------

class GrZipStr {
    function compress($str){
        $bits = $this->str2bits($str);
        $hex  = $this->bits2hex($bits);
        $bin  = pack("h*", $hex);

        return $bin;
    }

    function decompress($bin){
        list($junk, $bytes) = unpack("h*", $bin);
        $bits = $this->hex2bits($bytes);
        $str  = $this->bits2str($bits);

        return $str;
    }

    function search($bin, $word){
        $w_bits = $this->str2bits($word);

        list($junk, $bytes) = unpack("h*", $bin);
        $bits = $this->hex2bits($bytes);

        if(strpos($bits, $w_bits)){
            return 1;
        }else{
            return 0;
        }
    }

    function length($bin){
        list($junk, $bytes) = unpack("h*", $bin);
        $bits  = $this->hex2bits($bytes);

        $count = 0;
        $i     = 0;

        while(isset($bits[$i])){
            $count++;
            if($bits[$i] == 1){
                $i += 9;
            }else{
                $i += 4;
            }
        }

        return $count;
    }

    function str2bits($str){
        $bits  = '';
        $total = strlen($str);

        for($i=0; $i<$total; $i++){
            $char  = $str[$i];

            switch ($char) {
                case ' ':
                    $bits .= '0000';
                    break;
                case 'e':
                    $bits .= '0001';
                    break;
                case 'n':
                    $bits .= '0010';
                    break;
                case 'r':
                    $bits .= '0011';
                    break;
                case 'i':
                    $bits .= '0100';
                    break;
                case 's':
                    $bits .= '0101';
                    break;
                case 't':
                    $bits .= '0110';
                    break;
                case 'u':
                    $bits .= '0111';
                    break;
                default:
                    $int   = ord($char);
                    $bin   = base_convert($int, 10, 2);
                    $bits .= '1' . sprintf("%08d", $bin);
            }
        }

        // Complete nibbel
        $add   = strlen($bits) % 4;
        $bits .= str_repeat('0', $add);

        return $bits;
    }

    function bits2str($bits){
        $str = '';
        while($bits){
            $flag = substr($bits, 0, 1);
            $bits = substr($bits, 1);

            if ($flag == 1){
                $byte = substr($bits, 0, 8);
                $bits = substr($bits, 8);

                if($bits || strlen($code) == 8){
                    $int  = base_convert($byte, 2, 10);
                    $str .= chr($int);
                }
            }else{
                $code = substr($bits, 0, 3);
                $bits = substr($bits, 3);

                if($bits || strlen($code) == 3){
                    switch ($code) {
                        case '000':
                            $str .= ' ';
                            break;
                        case '001':
                            $str .= 'e';
                            break;
                        case '010':
                            $str .= 'n';
                            break;
                        case '011':
                            $str .= 'r';
                            break;
                        case '100':
                            $str .= 'i';
                            break;
                        case '101':
                            $str .= 's';
                            break;
                        case '110':
                            $str .= 't';
                            break;
                        case '111':
                            $str .= 'u';
                            break;
                    }
                }
            }
        }

        return $str;
    }

    function bits2hex($bits){
         $hex   = '';
         $total = strlen($bits)/4;

         for($i=0; $i<$total; $i++){
             $nibbel = substr($bits, 0, 4);
             $bits   = substr($bits, 4);

             $hex   .= base_convert($nibbel, 2, 16);
         }

         return $hex;
    }

    function hex2bits($hex){
         $bits  = '';
         $total = strlen($hex);

         for($i=0; $i<$total; $i++){
             $bits .= sprintf("%04d", base_convert($hex[$i], 16, 2));
         }

         return $bits;
    }
}
?>
Return current item: ArZipStr