<?php if (is_utf8($string)) $detect='utf-8'; else {
$utf8=0;
$koi8u=0;
$strlen=strlen($string); for($i=0;$i<$strlen;$i++) {
$code=ord($string[$i]); if (($code>223and $code<256) or ($code==179) or ($code==180) or ($code==186) or ($code==191)) $utf8++; // а-я, і, ґ, є, Ї if (($code>191and $code<224) or ($code==164) or ($code==166) or ($code==167) or ($code==173)) $koi8u++; // а-я, є, і, ї, ґ
} if ($utf8>$koi8u) $detect='windows-1251'; else $detect='koi8-u';
} if ($encoding==$detect) return $string; elsereturn iconv($detect, $encoding."//TRANSLIT", $string);
} ?>