Php array encoding and conversion methods fgetcsv Chinese garbled problem, when processing csv file import, fgetcsv gets Chinese garbled problem .. because the project uses UTF-8 encoding, the imported csv file contains Chinese garbled characters stored in ansi encoding.
The solution is as follows.
Reference: http://www.pignut.net/352
When processing the csv file import today, the array returned by the fgetcsv method contains all garbled characters. My analysis is because all the project files are UTF-8 encoded, and
The imported csv file is saved in ansi encoding. for the Chinese operating system environment, it should be gbk encoding. by manually changing the browser character encoding to gbk, garbled characters will disappear. Yu
Is determined to encode the array.
The system's default encoding and conversion function iconv can only accept string parameters. of course, we can use array_walk ...... Array methods can also be processed recursively using the callback function.
Directly read the array unit and then fill in the array. However, if the array is large or there are too many dimensions, you have to consider the performance comparison. Because the iconv function can only accept string parameters
To serialize the array by using the serialize function, convert and encode the array through iconv, and then use unserialize for deserialization. But I don't know why the deserialization fails after transcoding.
Come back, find a time to experiment separately. if you are clear, leave a message for me.
Ask for help from other Variable handling
Function. The var_export function can output or return a string representation of a variable. This function returns the structure information about the variables passed to this function. it is similar to var_dump ()
Similarly, the difference is that the returned representation is a valid PHP code. You can set the second parameter of the functionTRUETo return the expression of the variable. Next we will use this feature to convert the array into a string and then iconv and then use the eval function to restore it to an array. The result is displayed correctly.
$data = eval('return '.iconv('gbk','utf-8',var_export($data,true)).';');
$ Data is the array to be converted into encoding.