首页 > 解决方案 > 从 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 文件。

我需要帮助才能正确解析此文件。在此先感谢您的帮助!

标签: php

解决方案


我解决了这个问题:

$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);

推荐阅读