I18N_Arabic

# Source for file CompressStr.php

Documentation is available at CompressStr.php

1. <?php
2. /**
3. * ----------------------------------------------------------------------
4. *
5. * Copyright (c) 2006-2016 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-2016 Khaled Al-Sham'aa
111. *
112. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
113. * @link http://www.ar-php.org
114. */
116. /**
117. * This PHP class compress Arabic string using Huffman-like coding
118. *
119. * @category I18N
120. * @package I18N_Arabic
121. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
122. * @copyright 2006-2016 Khaled Al-Sham'aa
123. *
124. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
125. * @link http://www.ar-php.org
126. */
127. class I18N_Arabic_CompressStr
128. {
129. private static $_encode;
130. private static $_binary;
132. private static $_hex;
133. private static $_bin;
135. /**
136. * Loads initialize values
137. *
138. * @ignore
139. */
140. public function __construct()
141. {
142. self::$_encode = iconv('utf-8', 'cp1256', ' الميوتة');
143. self::$_binary = '0000|0001|0010|0011|0100|0101|0110|0111|';
145. self::$_hex = '0123456789abcdef';
146. self::$_bin = '0000|0001|0010|0011|0100|0101|0110|0111|1000|';
147. self::$_bin = self::$_bin . '1001|1010|1011|1100|1101|1110|1111|';
148. }
150. /**
151. * Set required encode and binary hash of most probably character in
152. * selected language
153. *
154. * @param string $lang [en, fr, gr, it, sp, ar] Language profile selected
155. *
156. * @return object $this to build a fluent interface
157. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
158. */
159. public function setLang($lang)
160. {
161. switch ($lang) {
162. case 'en':
163. self::$_encode = ' etaoins';
164. break;
165. case 'fr':
166. self::$_encode = ' enasriu';
167. break;
168. case 'gr':
169. self::$_encode = ' enristu';
170. break;
171. case 'it':
172. self::$_encode = ' eiaorln';
173. break;
174. case 'sp':
175. self::$_encode = ' eaosrin';
176. break;
177. default:
178. self::$_encode = iconv('utf-8', 'cp1256', ' الميوتة');
179. }
181. self::$_binary = '0000|0001|0010|0011|0100|0101|0110|0111|';
183. return $this;
184. }
186. /**
187. * Compress the given string using the Huffman-like coding
188. *
189. * @param string $str The text to compress
190. *
191. * @return binary The compressed string in binary format
192. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
193. */
194. public static function compress($str)
195. {
196. $str = iconv('utf-8', 'cp1256', $str);
198. $bits = self::str2bits($str);
199. $hex = self::bits2hex($bits);
200. $bin = pack('h*', $hex);
202. return $bin;
203. }
205. /**
206. * Uncompress a compressed string
207. *
208. * @param binary $bin The text compressed by compress().
209. *
210. * @return string The original uncompressed string
211. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
212. */
213. public static function decompress($bin)
214. {
215. $temp = unpack('h*', $bin);
216. $bytes = $temp[1];
218. $bits = self::hex2bits($bytes);
219. $str = self::bits2str($bits);
220. $str = iconv('cp1256', 'utf-8', $str);
222. return $str;
223. }
225. /**
226. * Search a compressed string for a given word
227. *
228. * @param binary $bin Compressed binary string
229. * @param string $word The string you looking for
230. *
231. * @return boolean True if found and False if not found
232. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
233. */
234. public static function search($bin, $word)
235. {
236. $word = iconv('utf-8', 'cp1256', $word);
237. $wBits = self::str2bits($word);
239. $temp = unpack('h*', $bin);
240. $bytes = $temp[1];
241. $bits = self::hex2bits($bytes);
243. if (strpos($bits, $wBits)) {
244. return true;
245. } else {
246. return false;
247. }
248. }
250. /**
251. * Retrieve the original string length
252. *
253. * @param binary $bin Compressed binary string
254. *
255. * @return integer Original string length
256. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
257. */
258. public static function length($bin)
259. {
260. $temp = unpack('h*', $bin);
261. $bytes = $temp[1];
262. $bits = self::hex2bits($bytes);
264. $count = 0;
265. $i = 0;
267. while (isset($bits[$i])) {
268. $count++;
269. if ($bits[$i] == 1) {
270. $i += 9;
271. } else {
272. $i += 4;
273. }
274. }
276. return $count;
277. }
279. /**
280. * Convert textual string into binary string
281. *
282. * @param string $str The textual string to convert
283. *
284. * @return binary The binary representation of textual string
285. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
286. */
287. protected static function str2bits($str)
288. {
289. $bits = '';
290. $total = strlen($str);
292. $i = -1;
293. while (++$i < $total) {
294. $char = $str[$i];
295. $pos = strpos(self::$_encode, $char);
297. if ($pos !== false) {
298. $bits .= substr(self::$_binary, $pos*5, 4);
299. } else {
300. $int = ord($char);
301. $bits .= '1'.substr(self::$_bin, (int)($int/16)*5, 4);
302. $bits .= substr(self::$_bin, ($int%16)*5, 4);
303. }
304. }
306. // Complete nibbel
307. $add = strlen($bits) % 4;
308. $bits .= str_repeat('0', $add);
310. return $bits;
311. }
313. /**
314. * Convert binary string into textual string
315. *
316. * @param binary $bits The binary string to convert
317. *
318. * @return string The textual representation of binary string
319. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
320. */
321. protected static function bits2str($bits)
322. {
323. $str = '';
324. while ($bits) {
325. $flag = substr($bits, 0, 1);
326. $bits = substr($bits, 1);
328. if ($flag == 1) {
329. $byte = substr($bits, 0, 8);
330. $bits = substr($bits, 8);
332. if ($bits || strlen($code) == 8) {
333. $int = base_convert($byte, 2, 10);
334. $char = chr($int);
335. $str .= $char;
336. }
337. } else {
338. $code = substr($bits, 0, 3);
339. $bits = substr($bits, 3);
341. if ($bits || strlen($code) == 3) {
342. $pos = strpos(self::$_binary, "0$code|");
343. $str .= substr(self::$_encode, $pos/5, 1);
344. }
345. }
346. }
348. return $str;
349. }
351. /**
352. * Convert binary string into hexadecimal string
353. *
354. * @param binary $bits The binary string to convert
355. *
356. * @return hexadecimal The hexadecimal representation of binary string
357. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
358. */
359. protected static function bits2hex($bits)
360. {
361. $hex = '';
362. $total = strlen($bits) / 4;
364. for ($i = 0; $i < $total; $i++) {
365. $nibbel = substr($bits, $i*4, 4);
367. $pos = strpos(self::$_bin, $nibbel);
368. $hex .= substr(self::$_hex, $pos/5, 1);
369. }
371. return $hex;
372. }
374. /**
375. * Convert hexadecimal string into binary string
376. *
377. * @param hexadecimal $hex The hexadezimal string to convert
378. *
379. * @return binary The binary representation of hexadecimal string
380. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
381. */
382. protected static function hex2bits($hex)
383. {
384. $bits = '';
385. $total = strlen($hex);
387. for ($i = 0; $i < $total; $i++) {
388. $pos = strpos(self::$_hex, $hex[$i]);
389. $bits .= substr(self::$_bin, $pos*5, 4);
390. }
392. return $bits;
393. }
394. }

Documentation generated on Fri, 01 Jan 2016 10:25:55 +0200 by phpDocumentor 1.4.0