Pluf Framework

Pluf Framework Commit Details


Date:2009-05-07 02:51:44 (15 years 7 months ago)
Author:Loic d'Anterroches
Branch:develop, master
Commit:8a7e834931d6c8660dc74120730247856bae5b10
Parents: 2ddd5e10ca049e42afc33ea58b4e422310ea7302
Message:Added Russian encoding detection method.

Changes:

File differences

src/Pluf/Text/UTF8.php
115115
116116
117117
118
119
120
121
122
123
124
125
126
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
118176
119177
120178
return true;
}
/**
* Detect if a string is in a Russian charset.
*
* This should be used when the mb_string detection encoding is
* failing. For example:
*
* <pre>
* $encoding = mb_detect_encoding($string, mb_detect_order(), true);
* if ($encoding == false) {
* $encoding = Pluf_Text_UTF8::detect_cyr_charset($string);
* }
* </pre>
*
* @link http://forum.php.su/topic.php?forum=1&topic=1346
*
* @param string
* @return string Possible Russian encoding
*/
public static function detect_cyr_charset($str)
{
$charsets = array(
'KOI8-R' => 0,
'Windows-1251' => 0,
'CP-866' => 0,
'ISO-8859-5' => 0,
'MacCyrillic' => 0
);
$length = strlen($str);
for ($i=0; $i<$length; $i++) {
$char = ord($str[$i]);
//non-russian characters
if ($char < 128 || $char > 256) continue;
//CP866
if (($char > 159 && $char < 176) || ($char > 223 && $char < 242))
$charsets['CP-866']+=3;
if (($char > 127 && $char < 160)) $charsets['CP-866']+=1;
//KOI8-R
if (($char > 191 && $char < 223)) $charsets['KOI8-R']+=3;
if (($char > 222 && $char < 256)) $charsets['KOI8-R']+=1;
//WIN-1251
if ($char > 223 && $char < 256) $charsets['Windows-1251']+=3;
if ($char > 191 && $char < 224) $charsets['Windows-1251']+=1;
//MAC
if ($char > 221 && $char < 255) $charsets['MacCyrillic']+=3;
if ($char > 127 && $char < 160) $charsets['MacCyrillic']+=1;
//ISO-8859-5
if ($char > 207 && $char < 240) $charsets['ISO-8859-5']+=3;
if ($char > 175 && $char < 208) $charsets['ISO-8859-5']+=1;
}
arsort($charsets);
return key($charsets);
}
/**

Archive Download the corresponding diff file

Branches

Tags

Number of commits:
Page rendered in 0.05499s using 13 queries.