I18N_Arabic
[ class tree: I18N_Arabic ] [ index: I18N_Arabic ] [ all elements ]

Source for file CompressStr.php

Documentation is available at CompressStr.php

  1. <?php
  2. /**
  3.  * ----------------------------------------------------------------------
  4.  *  
  5.  * Copyright (c) 2006-2012 Khaled Al-Sham'aa.
  6.  *  
  7.  * http://www.ar-php.org
  8.  *  
  9.  * PHP Version 5
  10.  *  
  11.  * ----------------------------------------------------------------------
  12.  *  
  13.  * LICENSE
  14.  *
  15.  * This program is open source product; you can redistribute it and/or
  16.  * modify it under the terms of the GNU Lesser General Public License (LGPL)
  17.  * as published by the Free Software Foundation; either version 3
  18.  * of the License, or (at your option) any later version.
  19.  *  
  20.  * This program is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  * GNU Lesser General Public License for more details.
  24.  *  
  25.  * You should have received a copy of the GNU Lesser General Public License
  26.  * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl.txt>.
  27.  *  
  28.  * ----------------------------------------------------------------------
  29.  *  
  30.  * Class Name: Compress string using Huffman-like coding
  31.  *  
  32.  * Filename:   CompressStr.php
  33.  *  
  34.  * Original    Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:    This class will compress given string in binary format
  37.  *             using variable-length code table (derived in a particular way
  38.  *             based on the estimated probability of occurrence for each
  39.  *             possible value of the source symbol) for encoding a source symbol
  40.  *              
  41.  * ----------------------------------------------------------------------
  42.  *  
  43.  * Arabic Compress String Class
  44.  *
  45.  * Compress string using Huffman-like coding
  46.  *
  47.  * This class compresses text strings into roughly 70% of their original size
  48.  * by benefit from using compact coding for most frequented letters in a given
  49.  * language. This algorithm associated with text language, so you will find 6
  50.  * different classes for the following languages: Arabic, English, French,
  51.  * German, Italian and Spanish language.
  52.  * 
  53.  * Benefits of this compress algorithm include:
  54.  * 
  55.  * - It is written in pure PHP code, so there is no need to any
  56.  *   PHP extensions to use it.
  57.  * - You can search in compressed string directly without any need uncompress
  58.  *   text before search in.
  59.  * - You can get original string length directly without need to uncompress
  60.  *   compressed text.
  61.  * 
  62.  * Note:
  63.  * Unfortunately text compressed using this algorithm lose the structure that
  64.  * normal zip algorithm used, so benefits from using ZLib functions on this
  65.  * text will be reduced.
  66.  * 
  67.  * There is another drawback, this algorithm working only on text from a given
  68.  * language, it does not working fine on binary files like images or PDF.
  69.  * 
  70.  * Example:
  71.  * <code>
  72.  * include('./I18N/Arabic.php');
  73.  * $obj = new I18N_Arabic('CompressStr');
  74.  * 
  75.  * $obj->setInputCharset('windows-1256');
  76.  * $obj->setOutputCharset('windows-1256');
  77.  * 
  78.  * $file = 'Compress/ar_example.txt';
  79.  * $fh   = fopen($file, 'r');
  80.  * $str  = fread($fh, filesize($file));
  81.  * fclose($fh);
  82.  * 
  83.  * $zip = $obj->compress($str);
  84.  * 
  85.  * $before = strlen($str);
  86.  * $after  = strlen($zip);
  87.  * $rate   = round($after * 100 / $before);
  88.  * 
  89.  * echo "String size before was: $before Byte<br>";
  90.  * echo "Compressed string size after is: $after Byte<br>";
  91.  * echo "Rate $rate %<hr>";
  92.  * 
  93.  * $str = $obj->decompress($zip);
  94.  * 
  95.  * if ($obj->search($zip, $word)) {
  96.  *     echo "Search for $word in zipped string and find it<hr>";
  97.  * } else {
  98.  *     echo "Search for $word in zipped string and do not find it<hr>";
  99.  * }
  100.  * 
  101.  * $len = $obj->length($zip);
  102.  * echo "Original length of zipped string is $len Byte<hr>";
  103.  * 
  104.  * echo '<div dir="rtl" align="justify">'.nl2br($str).'</div>';
  105.  * </code>
  106.  *                
  107.  * @category  I18N
  108.  * @package   I18N_Arabic
  109.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  110.  * @copyright 2006-2012 Khaled Al-Sham'aa
  111.  *    
  112.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  113.  * @link      http://www.ar-php.org
  114.  */
  115.  
  116. // New in PHP V5.3: Namespaces
  117. // namespace I18N\Arabic;
  118. // 
  119. // $obj = new I18N\Arabic\CompressStr();
  120. // 
  121. // use I18N\Arabic;
  122. // $obj = new Arabic\CompressStr();
  123. //
  124. // use I18N\Arabic\CompressStr as CompressStr;
  125. // $obj = new CompressStr();
  126.  
  127. /**
  128.  * This PHP class compress Arabic string using Huffman-like coding
  129.  *  
  130.  * @category  I18N
  131.  * @package   I18N_Arabic
  132.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  133.  * @copyright 2006-2012 Khaled Al-Sham'aa
  134.  *    
  135.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  136.  * @link      http://www.ar-php.org
  137.  */ 
  138. {
  139.     private static $_encode;
  140.     private static $_binary;
  141.     
  142.     private static $_hex;
  143.     private static $_bin;
  144.    
  145.     /**
  146.      * Loads initialize values
  147.      *
  148.      * @ignore
  149.      */         
  150.     public function __construct()
  151.     {
  152.         self::$_encode iconv('utf-8''cp1256'' الميوتة');
  153.         self::$_binary '0000|0001|0010|0011|0100|0101|0110|0111|';
  154.     
  155.         self::$_hex '0123456789abcdef';
  156.         self::$_bin '0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111|';
  157.     }
  158.  
  159.     /**
  160.      * Set required encode and binary hash of most probably character in
  161.      * selected language
  162.      *      
  163.      * @param string $lang [en, fr, gr, it, sp, ar] Language profile selected
  164.      *      
  165.      * @return object $this to build a fluent interface
  166.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  167.      */
  168.     public static function setLang($lang
  169.     {
  170.         switch ($lang{
  171.         case 'en':
  172.             self::$_encode ' etaoins';
  173.             break;
  174.         case 'fr':
  175.             self::$_encode ' enasriu';
  176.             break;
  177.         case 'gr':
  178.             self::$_encode ' enristu';
  179.             break;
  180.         case 'it':
  181.             self::$_encode ' eiaorln';
  182.             break;
  183.         case 'sp':
  184.             self::$_encode ' eaosrin';
  185.             break;
  186.         default:
  187.             self::$_encode iconv('utf-8''cp1256'' الميوتة');
  188.         }
  189.  
  190.         self::$_binary '0000|0001|0010|0011|0100|0101|0110|0111|';
  191.         
  192.         return $this;
  193.     }
  194.     
  195.     /**
  196.      * Compress the given string using the Huffman-like coding
  197.      *      
  198.      * @param string $str The text to compress
  199.      *                    
  200.      * @return binary The compressed string in binary format
  201.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  202.      */
  203.     public static function compress($str
  204.     {
  205.         $str  iconv('utf-8''cp1256'$str);
  206.  
  207.         $bits self::str2bits($str);
  208.         $hex  self::bits2hex($bits);
  209.         $bin  pack('h*'$hex);
  210.  
  211.         return $bin;
  212.     }
  213.  
  214.     /**
  215.      * Uncompress a compressed string
  216.      *       
  217.      * @param binary $bin The text compressed by compress().
  218.      *                    
  219.      * @return string The original uncompressed string
  220.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  221.      */
  222.     public static function decompress($bin
  223.     {
  224.         $temp  unpack('h*'$bin);
  225.         $bytes $temp[1];
  226.  
  227.         $bits self::hex2bits($bytes);
  228.         $str  self::bits2str($bits);
  229.         $str  iconv('cp1256''utf-8'$str);
  230.  
  231.         return $str;
  232.     }
  233.  
  234.     /**
  235.      * Search a compressed string for a given word
  236.      *      
  237.      * @param binary $bin  Compressed binary string
  238.      * @param string $word The string you looking for
  239.      *                    
  240.      * @return boolean True if found and False if not found
  241.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  242.      */
  243.     public static function search($bin$word
  244.     {
  245.         $wBits self::str2bits($word);
  246.  
  247.         $temp  unpack('h*'$bin);
  248.         $bytes $temp[1];
  249.         $bits  self::hex2bits($bytes);
  250.  
  251.         if (strpos($bits$wBits)) {
  252.             return true;
  253.         else {
  254.             return false;
  255.         }
  256.     }
  257.  
  258.     /**
  259.      * Retrieve the origenal string length
  260.      *      
  261.      * @param binary $bin Compressed binary string
  262.      *      
  263.      * @return integer Origenal string length
  264.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  265.      */
  266.     public static function length($bin
  267.     {
  268.         $temp  unpack('h*'$bin);
  269.         $bytes $temp[1];
  270.         $bits  self::hex2bits($bytes);
  271.  
  272.         $count 0;
  273.         $i     0;
  274.  
  275.         while (isset($bits[$i])) {
  276.             $count++;
  277.             if ($bits[$i== 1{
  278.                 $i += 9;
  279.             else {
  280.                 $i += 4;
  281.             }
  282.         }
  283.  
  284.         return $count;
  285.     }
  286.  
  287.     /**
  288.      * Convert textual string into binary string
  289.      *      
  290.      * @param string $str The textual string to convert
  291.      *       
  292.      * @return binary The binary representation of textual string
  293.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  294.      */
  295.     protected static function str2bits($str
  296.     {
  297.         $bits  '';
  298.         $total strlen($str);
  299.  
  300.         $i = -1;
  301.         while (++$i $total{
  302.             $char $str[$i];
  303.             $pos  strpos(self::$_encode$char);
  304.  
  305.             if ($pos !== false{
  306.                 $bits .= substr(self::$_binary$pos*54);
  307.             else {
  308.                 $int   ord($char);
  309.                 $bits .= '1'.substr(self::$_bin(int)($int/16)*54);
  310.                 $bits .= substr(self::$_bin($int%16)*54);
  311.             }
  312.         }
  313.  
  314.         // Complete nibbel
  315.         $add   strlen($bits4;
  316.         $bits .= str_repeat('0'$add);
  317.  
  318.         return $bits;
  319.     }
  320.  
  321.     /**
  322.      * Convert binary string into textual string
  323.      *      
  324.      * @param binary $bits The binary string to convert
  325.      *       
  326.      * @return string The textual representation of binary string
  327.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  328.      */
  329.     protected static function bits2str($bits
  330.     {
  331.         $str '';
  332.         while ($bits{
  333.             $flag substr($bits01);
  334.             $bits substr($bits1);
  335.  
  336.             if ($flag == 1{
  337.                 $byte substr($bits08);
  338.                 $bits substr($bits8);
  339.  
  340.                 if ($bits || strlen($code== 8{
  341.                     $int  base_convert($byte210);
  342.                     $char chr($int);
  343.                     $str .= $char;
  344.                 }
  345.             else {
  346.                 $code substr($bits03);
  347.                 $bits substr($bits3);
  348.  
  349.                 if ($bits || strlen($code== 3{
  350.                     $pos  strpos(self::$_binary"0$code|");
  351.                     $str .= substr(self::$_encode$pos/51);
  352.                 }
  353.             }
  354.         }
  355.  
  356.         return $str;
  357.     }
  358.  
  359.     /**
  360.      * Convert binary string into hexadecimal string
  361.      *      
  362.      * @param binary $bits The binary string to convert
  363.      *       
  364.      * @return hexadecimal The hexadecimal representation of binary string
  365.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  366.      */
  367.     protected static function bits2hex($bits
  368.     {
  369.         $hex   '';
  370.         $total strlen($bits4;
  371.  
  372.         for ($i 0$i $total$i++{
  373.             $nibbel substr($bits$i*44);
  374.  
  375.             $pos  strpos(self::$_bin$nibbel);
  376.             $hex .= substr(self::$_hex$pos/51);
  377.         }
  378.  
  379.         return $hex;
  380.     }
  381.  
  382.     /**
  383.      * Convert hexadecimal string into binary string
  384.      *      
  385.      * @param hexadecimal $hex The hexadezimal string to convert
  386.      *       
  387.      * @return binary The binary representation of hexadecimal string
  388.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  389.      */
  390.     protected static function hex2bits($hex
  391.     {
  392.         $bits  '';
  393.         $total strlen($hex);
  394.  
  395.         for ($i 0$i $total$i++{
  396.             $pos   strpos(self::$_hex$hex[$i]);
  397.             $bits .= substr(self::$_bin$pos*54);
  398.         }
  399.  
  400.         return $bits;
  401.     }
  402. }

Documentation generated on Wed, 29 Aug 2012 08:32:53 +0200 by phpDocumentor 1.4.0