php function detect_encoding string static list array ASCII utf-8 wind

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
function detect_encoding($string) {
static $list = array('ASCII', 'utf-8', 'windows-1251');
foreach ($list as $item) {
if (strcmp(@iconv($item, $item, $string), $string) == 0)
return $item;
}
return null;
}
echo detect_encoding(iconv("UTF-8", "CP1251", "хуй пизда"))."\n";
echo detect_encoding("хуй пизда")."\n";
echo detect_encoding("xuj pizda")."\n";
echo detect_encoding("&&")."\n";