天天看點

php_字元編碼淺談

a. 濡???娆蹭嬌??b2312缂???锛??d?php瑕?杈??哄ご锛?header(??Content-Type: text/html; charset=gb2312")锛?????椤甸?㈡坊??<meta http-equiv="Content-Type" content="text/html; charset=gb2312">锛???????浠剁??缂????煎?涓?NSI锛????ㄨ?頒?????寮?锛?????涓洪???╃???涓?NSI锛?瑕???婧???浠躲??
????b. 濡???娆蹭嬌??tf-8缂???锛??d?php瑕?杈??哄ご 锛?header(??Content-Type: text/html; charset=utf-8")锛?????椤甸?㈡坊??<meta http-equiv="Content-Type" content="text/html; charset=utf-8">锛???????浠剁??缂????煎?涓?tf-8??
 
??????杩???瑕??ㄥ??conv?芥?版???????ヨ???utf-8缂?????椤甸?㈣漿??gb2312锛? ???闆??????conv?芥?版??????杩??ョ???版??涓?杞????版??灏變???缂???????灏?涓?浜??? 璁╂?????蜂?濂戒?浼??匡??葷?涓?涓??ヨ??????ラ??杩???iconv?芥?扮??涓?涓?bug??iconv?ㄨ漿?㈠??绗???"??b2312?朵??洪????瑙e?蟲?規?寰?绠???锛?灏辨??????瑕?杞?????缂??????? "//IGNORE" 涔?灏辨??iconv?芥?扮??浜?涓????闆??锛?濡?涓?锛?
iconv("UTF-8","GB2312//IGNORE",$data)
 
ignore????????蹇界?ヨ漿?㈡?剁????璇?锛?濡???娌℃??ignore???幫?????璇ュ??绗????㈢??瀛?绗?覆?芥??娉?琚?淇?瀛???

 

1.?ョ??椤甸?㈣??ワ??????杈??虹???锛?
print_r(iconv_get_encoding("all"));
2.瀵規??瀛?绗?覆杩?琛?缂???杞????
echo iconv('utf-8','gb2312','??浠?');????????????????//??????浠?????tf8缂???杞???負gb2312缂???
**iconv(in_charset,outcharset//TRANSLIT//IGNORE,$string);//'TRANSLIT':濡????ㄨ??虹??缂????煎?涓?涓???????瀛?绗????浠ュ?ㄧ被浼肩???涓??ユ?懼??浠o?'IGNORE':濡???杈??烘?煎?缂???涓?涓?????瀛?绗?覆涓?????瀛?绗??訛??藉?璺寵?杩?琛????㈠??绗???缂??????????ㄨ漿??澶辮觸澶?浼?涓???杈??猴?浜х????璇???
3.灏?瀛?绗?覆杩?琛?缂???杞?????????ㄥ?ゆ??缂???绫誨??锛?浣???璇存????娌℃??iconv濂姐??锛?
mb_convert_encoding(????浠???, 'utf-8','gb2312' );????//灏?????浠?????b2312杞???負utf8
mb_convert_encoding(????浠???, 'utf-8');??????????????//灏?????浠???杞??㈡??utf8缂????煎?
$str = mb_convert_encoding($str, "EUC-JP", "auto");
$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");

**绗?涓?涓????拌???浠ユ??array()褰㈠?锛?.璁劇疆缂????煎?锛?
iconv_set_encoding("internal_encoding", "UTF-8");??//璁劇疆???ㄧ???涓?tf8
iconv_set_encoding("output_encoding", "ISO-8859-1");//璁劇疆杈??虹???涓?SO-8859-1璁劇疆??椤癸?锛?.input_encoding 锛?.output_encoding 锛?.internal_encoding
**ISO-8859-1缂???????瀛???缂???锛???涓??煎??SCII锛???Latin1??ISO-8859-1??????
锛?.?ョ??瀛?绗?覆缂????瑰?锛?
?煎?锛?string mb_detect_encoding(string$str[,mixed$encoding_list= mb_detect_order()[,bool$strict= false]] )
$str='缂????瑰?';
echo mb_detect_encoding($str);????//锛?UTF-8
echo mb_detect_encoding($str, "auto");


echo mb_detect_encoding($str, "JIS, eucjp-win, sjis-win");


$ary[] = "ASCII";
$ary[] = "JIS";
$ary[] = "EUC-JP";
echo mb_detect_encoding($str, $ary);锛?.?ョ????浠剁????瑰?锛?
$file = 'text3.txt';
echo getFileEncoding(file_get_contents($file));  // 杈???TF-16LE7.?ゆ??瀛?绗?覆??????????瀹??煎?缂???锛?
 
?煎?锛?bool??mb_check_encoding([string$var=NULL[,string$encoding= mb_internal_encoding()]] )
$string="\x00\x81";
$encoding="Shift_JIS";mb_check_encoding($string,$encoding)??????//:true
锛?.??涓???澶?涓???????瀛?绗????杞????
 
 
?煎?锛?stringmb_convert_variables(string$to_encoding,mixed$from_encoding,mixed&$vars[,mixed&$...] )
**$from_encoding:??浠ユ???闆??褰㈠?锛??ㄩ???峰????瀛?绗?覆??缁???浣?褰㈠???
$str1= '娴?璇?缂???';$str21= '娴?璇?缂???2';
$inputenc = mb_convert_variables("UTF-8", "UTF-8,GBK,GB2312", $str1, $str2);
var_dump($inputenc);????????//: string(5) "UTF-8"
var_dump($str1);????????????//: string(12) "娴?璇?缂???"
**CP936??GBK