/*
@params $str 输入字符 $type 所需获取编码
@author 长行
*/
function autoiconv($str,$type = "gb2312//ignore"){
$utf32_big_endian_bom = chr(0x00) . chr(0x00) . chr(0xfe) . chr(0xff);
$utf32_little_endian_bom = chr(0xff) . chr(0xfe) . chr(0x00) . chr(0x00);
$utf16_big_endian_bom = chr(0xfe) . chr(0xff);
$utf16_little_endian_bom = chr(0xff) . chr(0xfe);
$utf8_bom = chr(0xef) . chr(0xbb) . chr(0xbf);
$first2 = substr($str, 0, 2);
$first3 = substr($str, 0, 3);
$first4 = substr($str, 0, 3);
if ($first3 == $utf8_bom) $icon = 'utf-8';
elseif ($first4 == $utf32_big_endian_bom) $icon = 'utf-32be';
elseif ($first4 == $utf32_little_endian_bom) $icon = 'utf-32le';
elseif ($first2 == $utf16_big_endian_bom) $icon = 'utf-16be';
elseif ($first2 == $utf16_little_endian_bom) $icon = 'utf-16le';
else { $icon = 'ascii'; return $str;}
return iconv($icon,$type,$str);
}