[英]PHP read file with Japanese contents
I'm writing a php script in which I need to data from a CSV file in which some of the contents are written in Japanese.我正在编写一个 php 脚本,其中我需要从 CSV 文件中获取数据,其中一些内容是用日语编写的。 However, I can't get the data to read or display correctly at all.
但是,我根本无法正确读取或显示数据。
The file I'm reading is encoded in the iso-8859-1 charset.我正在阅读的文件以 iso-8859-1 字符集编码。 I also tried using iconv to convert it to a UTF-8 encoded file however doing that seemed to break the data in the file entirely, and the text wouldn't display correctly in any applications afterwards.
我还尝试使用 iconv 将其转换为 UTF-8 编码文件,但是这样做似乎完全破坏了文件中的数据,并且文本在之后的任何应用程序中都无法正确显示。
Here's the script I'm using right now:这是我现在正在使用的脚本:
<?php
header("Content-Type: text/html; charset=ISO-8859-1");
setlocale(LC_ALL, 'ja_JP.EUC-JP');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$row = 1;
if (($handle = fopen("/srv/http/Japanese/testFile.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$row++;
for ($i = 0; $i < 4; ++$i) {
echo $data[$i] . "<br />";
}
echo "<br />";
if ($row > 1000) break;
}
fclose($handle);
} else echo print_r(error_get_last(),true);
?>
</body>
</html>
The first two lines of PHP were added to try to fix the issue but it hasn't worked.添加了前两行 PHP 以尝试解决此问题,但没有奏效。
The output for a string in the file reading引き込む, 762, 762, 7122
comes out looking like this:读取
引き込む, 762, 762, 7122
文件中的字符串输出如下所示:
°ú¤¹þ¤à
762
762
7122
Also, it doesn't seem to be an issue solely with the display of the data.此外,这似乎不仅仅是数据显示的问题。 I also tried testing the data with
if ($data[$i]) == "引き込む")
and it seems to be false even when I do know that's the string being read.我还尝试使用
if ($data[$i]) == "引き込む")
测试数据,即使我知道这是正在读取的字符串,它似乎也是错误的。
I've also tried using other means of reading files, however no matter which PHP method I'm using to read the file I seem to get the exact same issue.我也尝试过使用其他读取文件的方法,但是无论我使用哪种 PHP 方法来读取文件,我似乎都遇到了完全相同的问题。
Any help would be greatly appreciated.任何帮助将不胜感激。
您需要将带有 iconv 的 csv 文件转换为 ja_JP.EUC-JP(并将元标记中的字符集值也设置为此值)或将 csv 转换为 utf8 并设置适当的字符集 (ja_JP.UTF8)。
I wanted to comment but I dont' have points so please forgive me if my answer is incorrect我想发表评论,但我没有积分,所以如果我的答案不正确,请原谅我
From what i can find on google and Stackoverflow this seems to be a solution you just have to fit it into you code从我在谷歌和 Stackoverflow 上可以找到的内容来看,这似乎是一个解决方案,您只需将其放入您的代码中
This code这段代码
setlocale(LC_ALL, 'ja_JP');
$data = array_map('str_getcsv', file('japanese.csv'));
var_dump($data);
works with the following CSV file (japanese.csv, saved in UTF-8) on my local.使用我本地的以下 CSV 文件(japanese.csv,以 UTF-8 保存)。
日本語,テスト,ファイル
2行目,CSV形式,エンコードUTF-8
The results are结果是
array(2) {
[0]=>
array(3) {
[0]=>
string(9) "日本語"
[1]=>
string(9) "テスト"
[2]=>
string(12) "ファイル"
}
[1]=>
array(3) {
[0]=>
string(7) "2行目"
[1]=>
string(9) "CSV形式"
[2]=>
string(20) "エンコードUTF-8"
}
}
this might help you understand more: Like to other post这可能会帮助您了解更多: 喜欢其他帖子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.