PHP Automatic Recognition code, if there is Chinese, it is best to convert it to UTF-8, because the Chinese in GBK editing situation, there may be garbled, this and client and service-side coding are related, in order to avoid garbled, we can use the following function to automatically convert it to UTF8 International standard Code:
| The code is as follows |
Copy Code |
<?php function Characet ($data) { if (!empty ($data)) { $fileType = mb_detect_encoding ($data, Array (' UTF-8 ', ' GBK ', ' LATIN1 ', ' BIG5 ')); if ($fileType!= ' UTF-8 ') { $data = mb_convert_encoding ($data, ' utf-8 ', $fileType); } } return $data; } ?> |
Supplemental: File Encoding Conversion
| The code is as follows |
Copy Code |
$temstr =file_get_contents ($path); $encode = mb_detect_encoding ($temstr, "ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW"); $temstr =mb_convert_encoding ($temstr, "CP936", $encode); |
Example
| code is as follows |
copy code |
| <?php $f =fopen ("Test.txt", "WB"); $text =utf8_encode ("a!"); ///Use function Utf8_encode to convert the data you want to write into a UTF encoding format. $text = "\\xEF\\xBB\\xBF". $text; //"\\xEF\\xBB\\xBF", this string of characters is indispensable , the resulting file will become the UTF-8 format, otherwise it is still in ANSI format. fputs ($f, $text); //write. fclose ($f); ? > |