A bug in the iconv function. Iconv will encounter an error when converting characters & mdash; to gb2312. the solution is very simple: add IGNORE (the second parameter of the iconv function) after the code to be converted
A bug in the iconv function. Iconv will encounter an error when converting the characters "-" to gb2312. the solution is very simple: add "// IGNORE" after the encoding to be converted, that is, after the second parameter of the iconv function. reference content is as follows:
Iconv ("UTF-8", "GB2312 // IGNORE", $ data)
Ignore indicates that the conversion error is ignored. without the ignore parameter, all strings after this character cannot be saved. this iconv () function is built-in in php5, the instance code is as follows:
-
- Echo $ str = 'Hi, it's coffee sale! ';
- Echo' ';
- Echo iconv ('gb2312', 'utf-8', $ str); // Encode the string from GB2312 to UTF-8
- Echo' ';
- Echo iconv_substr ($ str, 1, 1, 'utf-8'); // truncate by number of characters rather than bytes
- Print_r (iconv_get_encoding (); // Obtain the encoding information of the current page.
- Echo iconv_strlen ($ str, 'utf-8'); // you can specify the length of the encoded string.
- // This is also applicable.
- $ Content = iconv ("UTF-8", "gbk // transcoder", $ content );
- ?>
However, using the iconv function may encounter a problem such as notice: iconv () [function. iconv]: detected an illegal character in input string... the error occurs because the encoding range is incorrect. if gb2312 is smaller than gbk and is smaller than uft8, you must pay attention to this during conversion, however, php also provides a function mb_detect_encoding, which can better solve this problem. now we can write it into a more professional function.
-
- Function phpcharset ($ data, $ ){
- If (is_array ($ data )){
- Foreach ($ data as $ key => $ val ){
- $ Data [$ key] = phpcharset ($ val, $ );
- }
- } Else {
- $ Encode_array = array ('ascii ', 'utf-8', 'gbk', 'gb2312', 'big5 ');
- $ Encoded = mb_detect_encoding ($ data, $ encode_array );
- $ To = strtoupper ($ );
- If ($ encoded! = $ ){
- $ Data = mb_convert_encoding ($ data, $ to, $ encoded );
- }
- }
- Return $ data;
- }
- ?>
Sometimes we do not know the character encoding. in this case, we need to detect the encoding before conversion. the code is as follows:
- Function asciitog ($ brand)
- {
- $ Cha = mb_detect_encoding ($ brand );
- If ($ cha = 'utf-8 ')
- {
- $ Brand2 = iconv ($ cha, "gb2312", $ brand );
- }
- $ Cha2 = mb_detect_encoding ($ brand2 );
- If ($ cha2! = 'Ascii '){
- $ Brand = $ brand2;
- }
-
- Return $ brand;
- }