I18N_Arabic

# Source for file CharsetD.php

Documentation is available at CharsetD.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: Detect Arabic String Character Set
31. *
32. * Filename: CharsetD.php
33. *
34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
35. *
36. * Purpose: This class will return Arabic character set that used for
37. * a given Arabic string passing into this class, those available
38. * character sets that can be detected by this class includes
39. * the most popular three: Windows-1256, ISO 8859-6, and UTF-8.
40. *
41. * ----------------------------------------------------------------------
42. *
43. * Detect Arabic String Character Set
44. *
45. * The last step of the Information Retrieval process is to display the found
46. * documents to the user. However, some difficulties might occur at that point.
47. * English texts are usually written in the ASCII standard. Unlike the English
48. * language, many languages have different character sets, and do not have one
49. * standard. This plurality of standards causes problems, especially in a web
50. * environment.
51. *
52. * This PHP class will return Arabic character set that used for a given
53. * Arabic string passing into this class, those available character sets that can
54. * be detected by this class includes the most popular three: Windows-1256,
55. * ISO 8859-6, and UTF-8.
56. *
57. * Example:
58. * <code>
59. * include('./I18N/Arabic.php');
60. * $obj = new I18N_Arabic('CharsetD');
61. *
62. * $charset = $obj->getCharset($text);
63. * </code>
64. *
65. * @category I18N
66. * @package I18N_Arabic
67. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
68. * @copyright 2006-2016 Khaled Al-Sham'aa
69. *
70. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
71. * @link http://www.ar-php.org
72. */
74. /**
75. * This PHP class detect Arabic string character set
76. *
77. * @category I18N
78. * @package I18N_Arabic
79. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
80. * @copyright 2006-2016 Khaled Al-Sham'aa
81. *
82. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
83. * @link http://www.ar-php.org
84. */
85. class I18N_Arabic_CharsetD
86. {
87. /**
88. * Loads initialize values
89. *
90. * @ignore
91. */
92. public function __construct()
93. {
94. }
96. /**
97. * Count number of hits for the most frequented letters in Arabic language
98. * (Alef, Lam and Yaa), then calculate association ratio with each of
99. * possible character set (UTF-8, Windows-1256 and ISO-8859-6)
100. *
101. * @param String $string Arabic string in unknown format
102. *
103. * @return Array Character set as key and string association ratio as value
104. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
105. */
106. public function guess($string)
107. {
108. // The most frequent Arabic letters are Alef, Lam, and Yeh
109. $charset['windows-1256'] = substr_count($string, chr(199));
110. $charset['windows-1256'] += substr_count($string, chr(225));
111. $charset['windows-1256'] += substr_count($string, chr(237));
113. $charset['iso-8859-6'] = substr_count($string, chr(199));
114. $charset['iso-8859-6'] += substr_count($string, chr(228));
115. $charset['iso-8859-6'] += substr_count($string, chr(234));
117. $charset['utf-8'] = substr_count($string, chr(216).chr(167));
118. $charset['utf-8'] += substr_count($string, chr(217).chr(132));
119. $charset['utf-8'] += substr_count($string, chr(217).chr(138));
121. $total = $charset['windows-1256'] +
122. $charset['iso-8859-6'] +
123. $charset['utf-8'] + 1;
125. $charset['windows-1256'] = round($charset['windows-1256'] * 100 / $total);
126. $charset['iso-8859-6'] = round($charset['iso-8859-6'] * 100 / $total);
127. $charset['utf-8'] = round($charset['utf-8'] * 100 / $total);
129. return $charset;
130. }
132. /**
133. * Find the most possible character set for given Arabic string in unknown
134. * format
135. *
136. * @param String $string Arabic string in unknown format
137. *
138. * @return String The most possible character set for given Arabic string in
139. * unknown format[utf-8|windows-1256|iso-8859-6]
140. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
141. */
142. public function getCharset($string)
143. {
144. if (preg_match('/<meta .* charset=([^\"]+)".*>/sim', $string, $matches)) {
145. $value = $matches[1];
146. } else {
147. $charset = $this->guess($string);
148. arsort($charset);
149. $value = key($charset);
150. }
152. return $value;
153. }
154. }

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