php - 从 cav 文件 php 中读取和解析带有日语字符的英语
问题描述
我有一个 csv 文件,其中有很多这样的行:
I Want It All (Tribute to Queen);Dancer (おもしろ♪ Ver.)
Hijo De La Luna (Tribute to Mecano);Perfect (おもしろ♪ Ver.)
You've Got A Friend In Me (おもしろ♪英語 Ver.) [映画『トイ·ストーリー』より]
CSV 文件有两列。第一个仅包含英文字符串,但第二个包含英文和日文字符的混合。我读取此 csv 文件的代码:
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<?php
header('Content-Encoding: UTF-8');
$string = file_get_contents('myfile.csv');
echo $string;
?>
// My output
��I Want It All (Tribute to Queen);Dancer (J0�0W0�0j& Ver.)
Hijo De La Luna (Tribute to Mecano);Perfect (J0�0W0�0j& Ver.)
��You've Got A Friend In Me (J0�0W0�0j& Ver.) [ f;u0�0�0��0�0�0�0�00�0�0]
如果我尝试:
echo "Losing My Religion (Tribute to R.E.M.);I Love It (オモシロ♪ヴォイス ver.)"
它正确显示带有日语字符的文本。我尝试了在此站点上找到的所有解决方案,但无法正确解析 csv 文件。
我需要帮助才能正确解析此文件。在此先感谢您的帮助!
解决方案
我解决了这个问题:
$f = file_get_contents('myfile.csv'); // Get the whole file as string
$f = mb_convert_encoding($f, 'UTF8', 'UTF-16LE'); // Convert the file to UTF8
$f = preg_split("/\R/", $f); // Split it by line breaks
$f = array_map('str_getcsv', $f);
推荐阅读
- css - dijitt.Tree:仅对顶级条目进行样式设置
- c++ - 并行访问向量
- c++ - c++ readfile 读取文件不正确,
- java - 更改树视图的字体和大小
- ajaxform - 如果使用此 ajax 脚本,任何其他形式都不起作用
- java - 使用 RestAssured 后调用的 Java 安全异常
- .htaccess - 重写特定页面的查询字符串
- c++ - Boost buffer_cast 无法从 void* 转换为 PointerToPodType
- django - 使用 Multiple Databases 在 djnago 中调用二级数据库的模型
- reactjs - 如何从返回 axios 的响应对象访问 JWT 令牌值?