Php可以';用Shift-JIS编码读取文件
在服务器上运行时,我遇到了一个关于使用Shift_JIS编码读取csv文件的错误。我尝试按照以下代码进行操作:Php可以';用Shift-JIS编码读取文件,php,csv,Php,Csv,在服务器上运行时,我遇到了一个关于使用Shift_JIS编码读取csv文件的错误。我尝试按照以下代码进行操作: function readFile($file) { setlocale(LC_ALL, 'ja_JP.sjis'); $ret = array(); $fp = fopen($file, 'r'); while ($line = fgetcsv($fp)) { mb_convert_variables('utf-8', 'sjis-w
function readFile($file)
{
setlocale(LC_ALL, 'ja_JP.sjis');
$ret = array();
$fp = fopen($file, 'r');
while ($line = fgetcsv($fp)) {
mb_convert_variables('utf-8', 'sjis-win', $line);
$ret[] = $line;
}
fclose($fp);
return $ret;
}
在localhost中运行时,我并没有任何问题,但在服务器上运行时并没有正确的格式
这是在本地主机(XAMPP)上运行的文件的内容:
排列
(
)
但在服务器(centos)上运行:
排列
(
)
请帮帮我 如果可以更新PHP版本,那么您可以将PHP版本更新到至少5.6,然后问题就解决了 如果没有,请执行以下技巧:
function getFilePointerUTF8($target_file){
$current_locale = setlocale(LC_ALL, '0'); // Backup current locale.
setlocale(LC_ALL, 'ja_JP.UTF-8');
// Read the file content in SJIS-Win.
$content = file_get_contents($target_file);
// Convert file content to SJIS-Win.
$content = mb_convert_encoding($content, "UTF-8", "SJIS-win");
// Save the file as UTF-8 in a temp location.
$fp = tmpfile();
fwrite($fp, $content);
rewind($fp);
setlocale(LC_ALL, $current_locale); // Restore the backed-up locale.
return $fp;
}
并使用:
$fp = $this->getFileReaderUTF8("[your_file_location]"); // Ex: "~/data.csv"
$utf8_CSV_content = fgetcsv($fp);
有人能帮我吗?这对我来说很重要。我解决了这个问题,只需在服务器上将php版本升级到5.6。有没有任何方法可以将此应用到laravel excel和laravel版本5.2?
function getFilePointerUTF8($target_file){
$current_locale = setlocale(LC_ALL, '0'); // Backup current locale.
setlocale(LC_ALL, 'ja_JP.UTF-8');
// Read the file content in SJIS-Win.
$content = file_get_contents($target_file);
// Convert file content to SJIS-Win.
$content = mb_convert_encoding($content, "UTF-8", "SJIS-win");
// Save the file as UTF-8 in a temp location.
$fp = tmpfile();
fwrite($fp, $content);
rewind($fp);
setlocale(LC_ALL, $current_locale); // Restore the backed-up locale.
return $fp;
}
$fp = $this->getFileReaderUTF8("[your_file_location]"); // Ex: "~/data.csv"
$utf8_CSV_content = fgetcsv($fp);