Home >Backend Development >PHP Tutorial >PHP processing CSV---Chinese exception
Yesterday I used PHP to write a small program for processing csv files. It was processed normally on both the local and 21 servers
But after I put it on the 238 server at night, I found that the program processed abnormally and some data was missing.
After handling the exception, it was found that the missing data were all in Chinese.
At first I thought it was a file or string encoding problem
After using notepad++ to convert the csv file to utf-8, the problem still exists
Then
<span style="font-size:14px;">$date = mb_detect_encoding($content , array('UTF-8','GBK','LATIN1','BIG5')) ; $content = iconv($date, "utf-8", $content);</span>still failed to solve the problem
Then I did a little Baidu
and found that adding a line of code before processing the csv file: setlocale(LC_ALL,NULL)
setlocale() function sets regional information (regional information).
Regional information is language, currency, time and other information for a geographical area.
This function returns the current locale settings, or false if failed.
Note: The setlocale() function only changes the region information for the current script.
Tips: You can set the region information as the system default through setlocale(LC_ALL,NULL).
Record it here to avoid such problems in the future
The above introduces PHP's handling of CSV---Chinese exceptions, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.