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-2013 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-2013 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-2013 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|';
  157.         self::$_bin self::$_bin '1001|1010|1011|1100|1101|1110|1111|';
  158.     }
  159.  
  160.     /**
  161.      * Set required encode and binary hash of most probably character in
  162.      * selected language
  163.      *      
  164.      * @param string $lang [en, fr, gr, it, sp, ar] Language profile selected
  165.      *      
  166.      * @return object $this to build a fluent interface
  167.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  168.      */
  169.     public static function setLang($lang
  170.     {
  171.         switch ($lang{
  172.         case 'en':
  173.             self::$_encode ' etaoins';
  174.             break;
  175.         case 'fr':
  176.             self::$_encode ' enasriu';
  177.             break;
  178.         case 'gr':
  179.             self::$_encode ' enristu';
  180.             break;
  181.         case 'it':
  182.             self::$_encode ' eiaorln';
  183.             break;
  184.         case 'sp':
  185.             self::$_encode ' eaosrin';
  186.             break;
  187.         default:
  188.             self::$_encode iconv('utf-8''cp1256'' الميوتة');
  189.         }
  190.  
  191.         self::$_binary '0000|0001|0010|0011|0100|0101|0110|0111|';
  192.         
  193.         return $this;
  194.     }
  195.     
  196.     /**
  197.      * Compress the given string using the Huffman-like coding
  198.      *      
  199.      * @param string $str The text to compress
  200.      *                    
  201.      * @return binary The compressed string in binary format
  202.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  203.      */
  204.     public static function compress($str
  205.     {
  206.         $str  iconv('utf-8''cp1256'$str);
  207.  
  208.         $bits self::str2bits($str);
  209.         $hex  self::bits2hex($bits);
  210.         $bin  pack('h*'$hex);
  211.  
  212.         return $bin;
  213.     }
  214.  
  215.     /**
  216.      * Uncompress a compressed string
  217.      *       
  218.      * @param binary $bin The text compressed by compress().
  219.      *                    
  220.      * @return string The original uncompressed string
  221.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  222.      */
  223.     public static function decompress($bin
  224.     {
  225.         $temp  unpack('h*'$bin);
  226.         $bytes $temp[1];
  227.  
  228.         $bits self::hex2bits($bytes);
  229.         $str  self::bits2str($bits);
  230.         $str  iconv('cp1256''utf-8'$str);
  231.  
  232.         return $str;
  233.     }
  234.  
  235.     /**
  236.      * Search a compressed string for a given word
  237.      *      
  238.      * @param binary $bin  Compressed binary string
  239.      * @param string $word The string you looking for
  240.      *                    
  241.      * @return boolean True if found and False if not found
  242.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  243.      */
  244.     public static function search($bin$word
  245.     {
  246.         $wBits self::str2bits($word);
  247.  
  248.         $temp  unpack('h*'$bin);
  249.         $bytes $temp[1];
  250.         $bits  self::hex2bits($bytes);
  251.  
  252.         if (strpos($bits$wBits)) {
  253.             return true;
  254.         else {
  255.             return false;
  256.         }
  257.     }
  258.  
  259.     /**
  260.      * Retrieve the origenal string length
  261.      *      
  262.      * @param binary $bin Compressed binary string
  263.      *      
  264.      * @return integer Origenal string length
  265.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  266.      */
  267.     public static function length($bin
  268.     {
  269.         $temp  unpack('h*'$bin);
  270.         $bytes $temp[1];
  271.         $bits  self::hex2bits($bytes);
  272.  
  273.         $count 0;
  274.         $i     0;
  275.  
  276.         while (isset($bits[$i])) {
  277.             $count++;
  278.             if ($bits[$i== 1{
  279.                 $i += 9;
  280.             else {
  281.                 $i += 4;
  282.             }
  283.         }
  284.  
  285.         return $count;
  286.     }
  287.  
  288.     /**
  289.      * Convert textual string into binary string
  290.      *      
  291.      * @param string $str The textual string to convert
  292.      *       
  293.      * @return binary The binary representation of textual string
  294.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  295.      */
  296.     protected static function str2bits($str
  297.     {
  298.         $bits  '';
  299.         $total strlen($str);
  300.  
  301.         $i = -1;
  302.         while (++$i $total{
  303.             $char $str[$i];
  304.             $pos  strpos(self::$_encode$char);
  305.  
  306.             if ($pos !== false{
  307.                 $bits .= substr(self::$_binary$pos*54);
  308.             else {
  309.                 $int   ord($char);
  310.                 $bits .= '1'.substr(self::$_bin(int)($int/16)*54);
  311.                 $bits .= substr(self::$_bin($int%16)*54);
  312.             }
  313.         }
  314.  
  315.         // Complete nibbel
  316.         $add   strlen($bits4;
  317.         $bits .= str_repeat('0'$add);
  318.  
  319.         return $bits;
  320.     }
  321.  
  322.     /**
  323.      * Convert binary string into textual string
  324.      *      
  325.      * @param binary $bits The binary string to convert
  326.      *       
  327.      * @return string The textual representation of binary string
  328.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  329.      */
  330.     protected static function bits2str($bits
  331.     {
  332.         $str '';
  333.         while ($bits{
  334.             $flag substr($bits01);
  335.             $bits substr($bits1);
  336.  
  337.             if ($flag == 1{
  338.                 $byte substr($bits08);
  339.                 $bits substr($bits8);
  340.  
  341.                 if ($bits || strlen($code== 8{
  342.                     $int  base_convert($byte210);
  343.                     $char chr($int);
  344.                     $str .= $char;
  345.                 }
  346.             else {
  347.                 $code substr($bits03);
  348.                 $bits substr($bits3);
  349.  
  350.                 if ($bits || strlen($code== 3{
  351.                     $pos  strpos(self::$_binary"0$code|");
  352.                     $str .= substr(self::$_encode$pos/51);
  353.                 }
  354.             }
  355.         }
  356.  
  357.         return $str;
  358.     }
  359.  
  360.     /**
  361.      * Convert binary string into hexadecimal string
  362.      *      
  363.      * @param binary $bits The binary string to convert
  364.      *       
  365.      * @return hexadecimal The hexadecimal representation of binary string
  366.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  367.      */
  368.     protected static function bits2hex($bits
  369.     {
  370.         $hex   '';
  371.         $total strlen($bits4;
  372.  
  373.         for ($i 0$i $total$i++{
  374.             $nibbel substr($bits$i*44);
  375.  
  376.             $pos  strpos(self::$_bin$nibbel);
  377.             $hex .= substr(self::$_hex$pos/51);
  378.         }
  379.  
  380.         return $hex;
  381.     }
  382.  
  383.     /**
  384.      * Convert hexadecimal string into binary string
  385.      *      
  386.      * @param hexadecimal $hex The hexadezimal string to convert
  387.      *       
  388.      * @return binary The binary representation of hexadecimal string
  389.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  390.      */
  391.     protected static function hex2bits($hex
  392.     {
  393.         $bits  '';
  394.         $total strlen($hex);
  395.  
  396.         for ($i 0$i $total$i++{
  397.             $pos   strpos(self::$_hex$hex[$i]);
  398.             $bits .= substr(self::$_bin$pos*54);
  399.         }
  400.  
  401.         return $bits;
  402.     }
  403. }

Documentation generated on Mon, 14 Jan 2013 17:48:42 +0100 by phpDocumentor 1.4.0