Advertisement
Guest User

Untitled

a guest
Jun 17th, 2011
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. <?php  
  2. #функция определения кодировки текста
  3.     public function detect_encoding($string, $pattern_size = 50){
  4.     $list = array('cp1251', 'utf-8', 'ascii', '855', 'KOI8R', 'ISO-IR-111', 'CP866', 'KOI8U');
  5.     $c = strlen($string);
  6.     if ($c > $pattern_size){
  7.         $string = substr($string, floor(($c - $pattern_size) /2), $pattern_size);
  8.         $c = $pattern_size;
  9.     }
  10.  
  11.     $reg1 = '/(\xE0|\xE5|\xE8|\xEE|\xF3|\xFB|\xFD|\xFE|\xFF)/i';
  12.     $reg2 = '/(\xE1|\xE2|\xE3|\xE4|\xE6|\xE7|\xE9|\xEA|\xEB|\xEC|\xED|\xEF|\xF0|\xF1|\xF2|\xF4|\xF5|\xF6|\xF7|\xF8|\xF9|\xFA|\xFC)/i';
  13.  
  14.     $mk = 10000;
  15.     $enc = 'ascii';
  16.     foreach ($list as $item){
  17.         $sample1 = @iconv($item, 'cp1251', $string);
  18.         $gl = @preg_match_all($reg1, $sample1, $arr);
  19.         $sl = @preg_match_all($reg2, $sample1, $arr);
  20.         if (!$gl || !$sl) continue;
  21.         $k = abs(3 - ($sl / $gl));
  22.         $k += $c - $gl - $sl;
  23.         if ($k < $mk){
  24.             $enc = $item;
  25.             $mk = $k;
  26.         }
  27.     }
  28.     return $enc;
  29.     }
  30. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement