1 <?php
2 3 4 5 6
7
8
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 function utf8_is_ascii($str) {
30
31 return (preg_match('/(?:[^\x00-\x7F])/',$str) !== 1);
32 }
33
34
35 36 37 38 39 40 41 42 43 44
45 function utf8_is_ascii_ctrl($str) {
46 if ( strlen($str) > 0 ) {
47
48
49 return (preg_match('/[^\x09\x0A\x0D\x20-\x7E]/',$str) !== 1);
50 }
51 return FALSE;
52 }
53
54
55 56 57 58 59 60 61 62 63
64 function utf8_strip_non_ascii($str) {
65 ob_start();
66 while ( preg_match(
67 '/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S',
68 $str, $matches) ) {
69 if ( !isset($matches[2]) ) {
70 echo $matches[0];
71 }
72 $str = substr($str, strlen($matches[0]));
73 }
74 $result = ob_get_contents();
75 ob_end_clean();
76 return $result;
77 }
78
79
80 81 82 83 84 85 86 87 88
89 function utf8_strip_ascii_ctrl($str) {
90 ob_start();
91 while ( preg_match(
92 '/^([^\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)|([\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)/S',
93 $str, $matches) ) {
94 if ( !isset($matches[2]) ) {
95 echo $matches[0];
96 }
97 $str = substr($str, strlen($matches[0]));
98 }
99 $result = ob_get_contents();
100 ob_end_clean();
101 return $result;
102 }
103
104
105 106 107 108 109 110 111 112 113
114 function utf8_strip_non_ascii_ctrl($str) {
115 ob_start();
116 while ( preg_match(
117 '/^([\x09\x0A\x0D\x20-\x7E]+)|([^\x09\x0A\x0D\x20-\x7E]+)/S',
118 $str, $matches) ) {
119 if ( !isset($matches[2]) ) {
120 echo $matches[0];
121 }
122 $str = substr($str, strlen($matches[0]));
123 }
124 $result = ob_get_contents();
125 ob_end_clean();
126 return $result;
127 }
128
129
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
151 function utf8_accents_to_ascii( $str, $case=0 ){
152
153 static $UTF8_LOWER_ACCENTS = NULL;
154 static $UTF8_UPPER_ACCENTS = NULL;
155
156 if($case <= 0){
157
158 if ( is_null($UTF8_LOWER_ACCENTS) ) {
159 $UTF8_LOWER_ACCENTS = array(
160 'à' => 'a', 'ô' => 'o', 'ď' => 'd', 'ḟ' => 'f', 'ë' => 'e', 'š' => 's', 'ơ' => 'o',
161 'ß' => 'ss', 'ă' => 'a', 'ř' => 'r', 'ț' => 't', 'ň' => 'n', 'ā' => 'a', 'ķ' => 'k',
162 'ŝ' => 's', 'ỳ' => 'y', 'ņ' => 'n', 'ĺ' => 'l', 'ħ' => 'h', 'ṗ' => 'p', 'ó' => 'o',
163 'ú' => 'u', 'ě' => 'e', 'é' => 'e', 'ç' => 'c', 'ẁ' => 'w', 'ċ' => 'c', 'õ' => 'o',
164 'ṡ' => 's', 'ø' => 'o', 'ģ' => 'g', 'ŧ' => 't', 'ș' => 's', 'ė' => 'e', 'ĉ' => 'c',
165 'ś' => 's', 'î' => 'i', 'ű' => 'u', 'ć' => 'c', 'ę' => 'e', 'ŵ' => 'w', 'ṫ' => 't',
166 'ū' => 'u', 'č' => 'c', 'ö' => 'oe', 'è' => 'e', 'ŷ' => 'y', 'ą' => 'a', 'ł' => 'l',
167 'ų' => 'u', 'ů' => 'u', 'ş' => 's', 'ğ' => 'g', 'ļ' => 'l', 'ƒ' => 'f', 'ž' => 'z',
168 'ẃ' => 'w', 'ḃ' => 'b', 'å' => 'a', 'ì' => 'i', 'ï' => 'i', 'ḋ' => 'd', 'ť' => 't',
169 'ŗ' => 'r', 'ä' => 'ae', 'í' => 'i', 'ŕ' => 'r', 'ê' => 'e', 'ü' => 'ue', 'ò' => 'o',
170 'ē' => 'e', 'ñ' => 'n', 'ń' => 'n', 'ĥ' => 'h', 'ĝ' => 'g', 'đ' => 'd', 'ĵ' => 'j',
171 'ÿ' => 'y', 'ũ' => 'u', 'ŭ' => 'u', 'ư' => 'u', 'ţ' => 't', 'ý' => 'y', 'ő' => 'o',
172 'â' => 'a', 'ľ' => 'l', 'ẅ' => 'w', 'ż' => 'z', 'ī' => 'i', 'ã' => 'a', 'ġ' => 'g',
173 'ṁ' => 'm', 'ō' => 'o', 'ĩ' => 'i', 'ù' => 'u', 'į' => 'i', 'ź' => 'z', 'á' => 'a',
174 'û' => 'u', 'þ' => 'th', 'ð' => 'dh', 'æ' => 'ae', 'µ' => 'u', 'ĕ' => 'e',
175 );
176 }
177
178 $str = str_replace(
179 array_keys($UTF8_LOWER_ACCENTS),
180 array_values($UTF8_LOWER_ACCENTS),
181 $str
182 );
183 }
184
185 if($case >= 0){
186 if ( is_null($UTF8_UPPER_ACCENTS) ) {
187 $UTF8_UPPER_ACCENTS = array(
188 'À' => 'A', 'Ô' => 'O', 'Ď' => 'D', 'Ḟ' => 'F', 'Ë' => 'E', 'Š' => 'S', 'Ơ' => 'O',
189 'Ă' => 'A', 'Ř' => 'R', 'Ț' => 'T', 'Ň' => 'N', 'Ā' => 'A', 'Ķ' => 'K',
190 'Ŝ' => 'S', 'Ỳ' => 'Y', 'Ņ' => 'N', 'Ĺ' => 'L', 'Ħ' => 'H', 'Ṗ' => 'P', 'Ó' => 'O',
191 'Ú' => 'U', 'Ě' => 'E', 'É' => 'E', 'Ç' => 'C', 'Ẁ' => 'W', 'Ċ' => 'C', 'Õ' => 'O',
192 'Ṡ' => 'S', 'Ø' => 'O', 'Ģ' => 'G', 'Ŧ' => 'T', 'Ș' => 'S', 'Ė' => 'E', 'Ĉ' => 'C',
193 'Ś' => 'S', 'Î' => 'I', 'Ű' => 'U', 'Ć' => 'C', 'Ę' => 'E', 'Ŵ' => 'W', 'Ṫ' => 'T',
194 'Ū' => 'U', 'Č' => 'C', 'Ö' => 'Oe', 'È' => 'E', 'Ŷ' => 'Y', 'Ą' => 'A', 'Ł' => 'L',
195 'Ų' => 'U', 'Ů' => 'U', 'Ş' => 'S', 'Ğ' => 'G', 'Ļ' => 'L', 'Ƒ' => 'F', 'Ž' => 'Z',
196 'Ẃ' => 'W', 'Ḃ' => 'B', 'Å' => 'A', 'Ì' => 'I', 'Ï' => 'I', 'Ḋ' => 'D', 'Ť' => 'T',
197 'Ŗ' => 'R', 'Ä' => 'Ae', 'Í' => 'I', 'Ŕ' => 'R', 'Ê' => 'E', 'Ü' => 'Ue', 'Ò' => 'O',
198 'Ē' => 'E', 'Ñ' => 'N', 'Ń' => 'N', 'Ĥ' => 'H', 'Ĝ' => 'G', 'Đ' => 'D', 'Ĵ' => 'J',
199 'Ÿ' => 'Y', 'Ũ' => 'U', 'Ŭ' => 'U', 'Ư' => 'U', 'Ţ' => 'T', 'Ý' => 'Y', 'Ő' => 'O',
200 'Â' => 'A', 'Ľ' => 'L', 'Ẅ' => 'W', 'Ż' => 'Z', 'Ī' => 'I', 'Ã' => 'A', 'Ġ' => 'G',
201 'Ṁ' => 'M', 'Ō' => 'O', 'Ĩ' => 'I', 'Ù' => 'U', 'Į' => 'I', 'Ź' => 'Z', 'Á' => 'A',
202 'Û' => 'U', 'Þ' => 'Th', 'Ð' => 'Dh', 'Æ' => 'Ae', 'Ĕ' => 'E',
203 );
204 }
205 $str = str_replace(
206 array_keys($UTF8_UPPER_ACCENTS),
207 array_values($UTF8_UPPER_ACCENTS),
208 $str
209 );
210 }
211
212 return $str;
213
214 }
215