1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Language
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Class to transliterate strings
14 *
15 * @since 11.1
16 * @note Port of phputf8's utf8_accents_to_ascii()
17 */
18 class JLanguageTransliterate
19 {
20 /**
21 * Returns strings transliterated from UTF-8 to Latin
22 *
23 * @param string $string String to transliterate
24 * @param integer $case Optionally specify upper or lower case. Default to null.
25 *
26 * @return string Transliterated string
27 *
28 * @since 11.1
29 */
30 public static function utf8_latin_to_ascii($string, $case = 0)
31 {
32 static $UTF8_LOWER_ACCENTS = null;
33 static $UTF8_UPPER_ACCENTS = null;
34
35 if ($case <= 0)
36 {
37 if (is_null($UTF8_LOWER_ACCENTS))
38 {
39 $UTF8_LOWER_ACCENTS = array(
40 'à' => 'a',
41 'ô' => 'o',
42 'ď' => 'd',
43 'ḟ' => 'f',
44 'ë' => 'e',
45 'š' => 's',
46 'ơ' => 'o',
47 'ß' => 'ss',
48 'ă' => 'a',
49 'ř' => 'r',
50 'ț' => 't',
51 'ň' => 'n',
52 'ā' => 'a',
53 'ķ' => 'k',
54 'ŝ' => 's',
55 'ỳ' => 'y',
56 'ņ' => 'n',
57 'ĺ' => 'l',
58 'ħ' => 'h',
59 'ṗ' => 'p',
60 'ó' => 'o',
61 'ú' => 'u',
62 'ě' => 'e',
63 'é' => 'e',
64 'ç' => 'c',
65 'ẁ' => 'w',
66 'ċ' => 'c',
67 'õ' => 'o',
68 'ṡ' => 's',
69 'ø' => 'o',
70 'ģ' => 'g',
71 'ŧ' => 't',
72 'ș' => 's',
73 'ė' => 'e',
74 'ĉ' => 'c',
75 'ś' => 's',
76 'î' => 'i',
77 'ű' => 'u',
78 'ć' => 'c',
79 'ę' => 'e',
80 'ŵ' => 'w',
81 'ṫ' => 't',
82 'ū' => 'u',
83 'č' => 'c',
84 'ö' => 'oe',
85 'è' => 'e',
86 'ŷ' => 'y',
87 'ą' => 'a',
88 'ł' => 'l',
89 'ų' => 'u',
90 'ů' => 'u',
91 'ş' => 's',
92 'ğ' => 'g',
93 'ļ' => 'l',
94 'ƒ' => 'f',
95 'ž' => 'z',
96 'ẃ' => 'w',
97 'ḃ' => 'b',
98 'å' => 'a',
99 'ì' => 'i',
100 'ï' => 'i',
101 'ḋ' => 'd',
102 'ť' => 't',
103 'ŗ' => 'r',
104 'ä' => 'ae',
105 'í' => 'i',
106 'ŕ' => 'r',
107 'ê' => 'e',
108 'ü' => 'ue',
109 'ò' => 'o',
110 'ē' => 'e',
111 'ñ' => 'n',
112 'ń' => 'n',
113 'ĥ' => 'h',
114 'ĝ' => 'g',
115 'đ' => 'd',
116 'ĵ' => 'j',
117 'ÿ' => 'y',
118 'ũ' => 'u',
119 'ŭ' => 'u',
120 'ư' => 'u',
121 'ţ' => 't',
122 'ý' => 'y',
123 'ő' => 'o',
124 'â' => 'a',
125 'ľ' => 'l',
126 'ẅ' => 'w',
127 'ż' => 'z',
128 'ī' => 'i',
129 'ã' => 'a',
130 'ġ' => 'g',
131 'ṁ' => 'm',
132 'ō' => 'o',
133 'ĩ' => 'i',
134 'ù' => 'u',
135 'į' => 'i',
136 'ź' => 'z',
137 'á' => 'a',
138 'û' => 'u',
139 'þ' => 'th',
140 'ð' => 'dh',
141 'æ' => 'ae',
142 'µ' => 'u',
143 'ĕ' => 'e',
144 'œ' => 'oe',
145 );
146 }
147
148 $string = str_replace(array_keys($UTF8_LOWER_ACCENTS), array_values($UTF8_LOWER_ACCENTS), $string);
149 }
150
151 if ($case >= 0)
152 {
153 if (is_null($UTF8_UPPER_ACCENTS))
154 {
155 $UTF8_UPPER_ACCENTS = array(
156 'À' => 'A',
157 'Ô' => 'O',
158 'Ď' => 'D',
159 'Ḟ' => 'F',
160 'Ë' => 'E',
161 'Š' => 'S',
162 'Ơ' => 'O',
163 'Ă' => 'A',
164 'Ř' => 'R',
165 'Ț' => 'T',
166 'Ň' => 'N',
167 'Ā' => 'A',
168 'Ķ' => 'K',
169 'Ŝ' => 'S',
170 'Ỳ' => 'Y',
171 'Ņ' => 'N',
172 'Ĺ' => 'L',
173 'Ħ' => 'H',
174 'Ṗ' => 'P',
175 'Ó' => 'O',
176 'Ú' => 'U',
177 'Ě' => 'E',
178 'É' => 'E',
179 'Ç' => 'C',
180 'Ẁ' => 'W',
181 'Ċ' => 'C',
182 'Õ' => 'O',
183 'Ṡ' => 'S',
184 'Ø' => 'O',
185 'Ģ' => 'G',
186 'Ŧ' => 'T',
187 'Ș' => 'S',
188 'Ė' => 'E',
189 'Ĉ' => 'C',
190 'Ś' => 'S',
191 'Î' => 'I',
192 'Ű' => 'U',
193 'Ć' => 'C',
194 'Ę' => 'E',
195 'Ŵ' => 'W',
196 'Ṫ' => 'T',
197 'Ū' => 'U',
198 'Č' => 'C',
199 'Ö' => 'Oe',
200 'È' => 'E',
201 'Ŷ' => 'Y',
202 'Ą' => 'A',
203 'Ł' => 'L',
204 'Ų' => 'U',
205 'Ů' => 'U',
206 'Ş' => 'S',
207 'Ğ' => 'G',
208 'Ļ' => 'L',
209 'Ƒ' => 'F',
210 'Ž' => 'Z',
211 'Ẃ' => 'W',
212 'Ḃ' => 'B',
213 'Å' => 'A',
214 'Ì' => 'I',
215 'Ï' => 'I',
216 'Ḋ' => 'D',
217 'Ť' => 'T',
218 'Ŗ' => 'R',
219 'Ä' => 'Ae',
220 'Í' => 'I',
221 'Ŕ' => 'R',
222 'Ê' => 'E',
223 'Ü' => 'Ue',
224 'Ò' => 'O',
225 'Ē' => 'E',
226 'Ñ' => 'N',
227 'Ń' => 'N',
228 'Ĥ' => 'H',
229 'Ĝ' => 'G',
230 'Đ' => 'D',
231 'Ĵ' => 'J',
232 'Ÿ' => 'Y',
233 'Ũ' => 'U',
234 'Ŭ' => 'U',
235 'Ư' => 'U',
236 'Ţ' => 'T',
237 'Ý' => 'Y',
238 'Ő' => 'O',
239 'Â' => 'A',
240 'Ľ' => 'L',
241 'Ẅ' => 'W',
242 'Ż' => 'Z',
243 'Ī' => 'I',
244 'Ã' => 'A',
245 'Ġ' => 'G',
246 'Ṁ' => 'M',
247 'Ō' => 'O',
248 'Ĩ' => 'I',
249 'Ù' => 'U',
250 'Į' => 'I',
251 'Ź' => 'Z',
252 'Á' => 'A',
253 'Û' => 'U',
254 'Þ' => 'Th',
255 'Ð' => 'Dh',
256 'Æ' => 'Ae',
257 'Ĕ' => 'E',
258 'Œ' => 'Oe',
259 );
260 }
261
262 $string = str_replace(array_keys($UTF8_UPPER_ACCENTS), array_values($UTF8_UPPER_ACCENTS), $string);
263 }
264
265 return $string;
266 }
267 }
268