启用使用fgetcsv在浏览器中提取和显示外语内容 [英] Enable Extract and Display Foreign Language Content in browsers using fgetcsv

查看:278
本文介绍了启用使用fgetcsv在浏览器中提取和显示外语内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


iD;英语[en];中文[zh];德文[de];印地文[hi];印地语(TOGO)[hi_TG];日语[ja]
来源[ local] .AlarmGroup [AlarmText_02] .ID [1310:90];解开卷轴:Accu位置差太大。检查层压板断裂;拆卷器:蓄存器位置差过大。检查复合片材是否中断;层压机:Zu grossePositionsänderung - Auf Laminatrissprüfen; 290 ;;巻出装置:アキュムレータッドでが大きすぎます。ラれていいいいいいいいいいいい
源[local] .AlarmGroup [AlarmText_02] .ID [1311:91];解绕器:Accu级太小,无法自动拼接;拆卷器:自动拼接的存储器级别过小; Abwickler:AkkuFüllstandzu kleinfürAutospleiss; 291 ;;巻出装置:自动纸で行うよいレベルが小さすぎます


我试图获取如上所述的csv内容:



csv文件保存为Unicode文本。它有中文,德语,日语。
我无法以正确的格式获取外语。



CSV阅读器代码



  header('Content-Type:text / html; charset = utf-8'); 
$ row = 1;
$ up_file ='text_SHOT_S.csv';
setlocale(LC_ALL,'en_US.UTF-8');
if(($ handle = fopen($ up_file,r))!== FALSE){
while(($ data = fgetcsv($ handle,1000,;))! = FALSE){
$ num = count($ data);
$ row ++;
for($ c = 0; $ c <$ num; $ c ++){
echo $ data [$ c]。'< br>';
}
}
fclose($ handle);}



输出以下代码:




iD戼㹲䔀䔀渀渀渀氀氀㰀㰀嬀㰀㰀㰀㰀㹲䜀爀爀洀愀渀嬀搀㰀㰀㰀㰀㰀㰀㰀㰀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀甀拆卸:拆卸:Accu(拆卸):拆卸:拆卸:拆卸:拆卸:拆卸:拆卸:拆卸:拆卸:拆卸:拆卸位置差太大。检查层压打破戼㹲였睢桓ᩖ⃿쐀墄桛䵖湏읝➏ə‰쀀1࡙䝔偲⽧♦ⵔ굎㱥牢> Laminatspeicher:祖GROSSEPositionsänderung - 奥夫Laminatrissprüfen戼㹲㈀㤀㰀牢>戼㹲FF艹앑沩㩿ꈀ괰فم뼰فم䴰湏湿䱝✰䵙夰丰縰夰Ȱ‰촰فم젰䰰쨰豘是䐰樰䐰䬰먰赸垊是估怰唰䐰ര㰀牢>源[本地] .AlarmGroup [AlarmText_02] .ID [1311:91]戼㹲唀渀眀碗渀搀攀爀㨀䄀挀挀甀氀攀瘀攀氀琀漀漀猀洀愀氀氀昀漀爀愀甀琀漀猀瀀氀碗挀攀㰀牢>拆卷器:自动拼接的蓄存器级别过小戼㹲䄀恋眀碗挀欀氀攀爀㨀䄀欀欀甀䘀ئج氀氀猀琀愀渀搀稀甀欀氀攀碗渀昀ئج爀䄀甀琀漀猀瀀氀攀碗猀猀㰀牢> 291戼㹲㰀牢>巻出装置:自动纸継を行うにはアキュムレーターのレベルが小さすぎます戼㹲㰀牢


我检查垃圾字符或大部分转换为中文的内容。
也尝试了头('Content-Type:text / html; charset = iso-8859-1') setlocale(LC_CTYPE ,'zh_CN.UTF-8','zh_ZH.big5');



我想要的输出与CSV内容相同。



提前感谢。

解决方案

为了阅读CSV内容,我使用PHPExcel并转换



请参考以下链接将UTF-16文件转换为UTF-8。



如何使用PHP将UTF-16文件转换为UTF-8文件


要转换文件,只需调用 convert_file_to_utf8()函数
并传递给文件的文件路径你想转换。
函数然后使用PHP函数 file_get_contents()
输入文件的内容打包成一个字符串变量,然后传递给
主转换器函数,将字符串从UTF-16转换为
UTF-8编码(如果需要)。最后,它使用 file_put_contents()
将生成的字符串填充回原始文件,重写
原始文件内容。




 函数utf16_to_utf8($ STR){

$ C0 = ORD($海峡[ 0]);
$ c1 = ord($ str [1]);

if($ c0 == 0xFE& $ c1 == 0xFF){
$ be = true;
} else if($ c0 == 0xFF& $ amp; $ c1 == 0xFE){
$ be = false;
} else {
return $ str;
}

$ str = substr($ str,2);
$ len = strlen($ str);
$ dec ='';
for($ i = 0; $ i <$ len; $ i + = 2){
$ c =($ be)? ord($ str [$ i])<< 8 | ord($ str [$ i + 1]):
ord($ str [$ i + 1])< 8 | ord($ str [$ i]);
if($ c> = 0x0001& $ c< = 0x007F){
$ dec。= chr($ c);
} else if($ c> 0x07FF){
$ dec。= chr(0xE0 |(($ c>> 12)& 0x0F)
$ dec。= chr(0x80 |(($ c> 6)& 0x3F));
$ dec。= chr(0x80 |(($ c>> 0)& 0x3F));
} else {
$ dec。= chr(0xC0 |(($ c> 6)& 0x1F));
$ dec。= chr(0x80 |(($ c>> 0)& 0x3F));
}
}
return $ dec;
}

function convert_file_to_utf8($ csvfile){

$ utfcheck = file_get_contents($ csvfile);
$ utfcheck = utf16_to_utf8($ utfcheck);
file_put_contents($ csvfile,$ utfcheck);
}


iD;English [en];Chinese [zh];German [de];Hindi [hi];Hindi (TOGO) [hi_TG];Japanese [ja] Source[local].AlarmGroup[AlarmText_02].ID[1310:90];Unwinder: Accu position difference too big. Check for laminate break;拆卷器: 蓄存器位置差过大。 检查复合片材是否中断;Laminatspeicher: Zu grosse Positionsänderung - Auf Laminatriss prüfen;290;;巻出装置: アキュムレーター位置の差が大きすぎます。 ラミネートが壊れていないか確認してください Source[local].AlarmGroup[AlarmText_02].ID[1311:91];Unwinder: Accu level too small for auto splice;拆卷器: 自动拼接的蓄存器级别过小;Abwickler: Akku Füllstand zu klein für Autospleiss;291;;巻出装置: 自動紙継を行うにはアキュムレーターのレベルが小さすぎます

I am trying to fetch csv content as mentioned above :

The csv file is saved as Unicode Text. It has Chinese, German, Japanese Language. I am unable to fetch foreign language in correct format.

CSV reader Code

header('Content-Type: text/html; charset=utf-8');
$row = 1;
$up_file = 'text_SHOT_S.csv';
setlocale(LC_ALL, 'en_US.UTF-8');
if (($handle = fopen($up_file, "r")) !== FALSE) {   
     while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
           echo $data[$c].'<br>';
        }
    }
    fclose($handle);}

Output of the following Code:

iD戼㹲䔀渀最氀椀猀栀 嬀攀渀崀㰀牢>Chinese [zh]戼㹲䜀攀爀洀愀渀 嬀搀攀崀㰀牢>Hindi [hi]戼㹲䠀椀渀搀椀 ⠀吀伀䜀伀⤀ 嬀栀椀开吀䜀崀㰀牢>Japanese [ja] 戼㹲匀漀甀爀挀攀嬀氀漀挀愀氀崀⸀䄀氀愀爀洀䜀爀漀甀瀀嬀䄀氀愀爀洀吀攀砀琀开 ㈀崀⸀䤀䐀嬀㄀㌀㄀ 㨀㤀 崀㰀牢>Unwinder: Accu position difference too big. Check for laminate break戼㹲였睢桓ᩖ⃿쐀墄桛䵖湏읝➏ə‰쀀൧࡙䝔偲⽧♦ⵔ굎㱥牢>Laminatspeicher: Zu grosse Positionsänderung - Auf Laminatriss prüfen戼㹲㈀㤀 㰀牢>戼㹲ff艹앑溈㩿  ꈀ괰ﰰ뼰ﰰ䴰湏湿䱝✰䵙夰丰縰夰Ȱ‰�촰ﰰ젰䰰쨰豘昰䐰樰䐰䬰먰赸垊昰估怰唰䐰ര㰀牢>Source[local].AlarmGroup[AlarmText_02].ID[1311:91]戼㹲唀渀眀椀渀搀攀爀㨀 䄀挀挀甀 氀攀瘀攀氀 琀漀漀 猀洀愀氀氀 昀漀爀 愀甀琀漀 猀瀀氀椀挀攀㰀牢>拆卷器: 自动拼接的蓄存器级别过小戼㹲䄀戀眀椀挀欀氀攀爀㨀 䄀欀欀甀 䘀ﰀ氀氀猀琀愀渀搀 稀甀 欀氀攀椀渀 昀ﰀ爀 䄀甀琀漀猀瀀氀攀椀猀猀㰀牢>291戼㹲㰀牢>巻出装置: 自動紙継を行うにはアキュムレーターのレベルが小さすぎます 戼㹲㰀牢

I either check garbage character or most of the content converted to Chinese. Also tried the header('Content-Type: text/html; charset=iso-8859-1') and setlocale(LC_CTYPE, 'zh_CN.UTF-8','zh_ZH.big5');

I want the output same as CSV content.

Thanks in advance .

解决方案

For reading CSV content I used PHPExcel and converted UTF-16 file into UTF-8 then it will fetch Chinese content properly.

Please refer below link for converting UTF-16 File to an UTF-8.

How to Convert an UTF-16 File to an UTF-8 file using PHP

To convert a file simply call the convert_file_to_utf8() function and pass to it the file path of the file you wish to convert. The function then uses the PHP function file_get_contents() to pack the input file’s contents into a string variable which is then passed to the main converter function which converts the string from UTF-16 to UTF-8 encoding if necessary. Finally, it uses file_put_contents() to stuff the resulting string back into the original file, overwriting the original file contents.

function utf16_to_utf8($str) {

    $c0 = ord($str[0]);
    $c1 = ord($str[1]);

    if ($c0 == 0xFE && $c1 == 0xFF) {
        $be = true;
    } else if ($c0 == 0xFF && $c1 == 0xFE) {
        $be = false;
    } else {
        return $str;
    }

    $str = substr($str, 2);
    $len = strlen($str);
    $dec = '';
    for ($i = 0; $i < $len; $i += 2) {
        $c = ($be) ? ord($str[$i]) << 8 | ord($str[$i + 1]) :
                ord($str[$i + 1]) << 8 | ord($str[$i]);
        if ($c >= 0x0001 && $c <= 0x007F) {
            $dec .= chr($c);
        } else if ($c > 0x07FF) {
            $dec .= chr(0xE0 | (($c >> 12) & 0x0F));
            $dec .= chr(0x80 | (($c >> 6) & 0x3F));
            $dec .= chr(0x80 | (($c >> 0) & 0x3F));
        } else {
            $dec .= chr(0xC0 | (($c >> 6) & 0x1F));
            $dec .= chr(0x80 | (($c >> 0) & 0x3F));
        }
    }
    return $dec;
}

function convert_file_to_utf8($csvfile) {

    $utfcheck = file_get_contents($csvfile);
    $utfcheck = utf16_to_utf8($utfcheck);
    file_put_contents($csvfile, $utfcheck);
}

这篇关于启用使用fgetcsv在浏览器中提取和显示外语内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆