如何使用php读取doc文件(具有不同的语言字符,例如日语)

时间:2014-06-14 07:37:33

标签: php

好吧,我正在使用以下PHP代码显示上传的文档内容。如果文档有英语,那么它显示完美,但是当我尝试上传不同的语言时.e。 g日语然后它显示以下字符。为什么它会显示这些不同的角色?

w��RL}tk�f R��\0�k�B0�0D0�00_0......................

Php代码:

<?php
function getPlainTextFromDocFile($filename){
if ( file_exists($filename) ) {
if ( ($fh = fopen($filename, 'r')) !== false ) {
$headers = fread($fh, 0xA00);
# 1 = (ord(n)*1) ; Document has from 0 to 255 characters
$n1 = ( ord($headers[0x21C]) - 1 );

# 1 = ((ord(n)-8)*256) ; Document has from 256 to 63743 characters
$n2 = ( ( ord($headers[0x21D]) - 8 ) * 256 );

# 1 = ((ord(n)*256)*256) ; Document has from 63744 to 16775423 characters
$n3 = ( ( ord($headers[0x21E]) * 256 ) * 256 );

# (((ord(n)*256)*256)*256) ; Document has from 16775424 to 4294965504 characters
$n4 = ( ( ( ord($headers[0x21F]) * 256 ) * 256 ) * 256 );

# Total length of text in the document
$textLength = ($n1 + $n2 + $n3 + $n4);

$extracted_plaintext = fread($fh, $textLength);

# if you want the plain text with no formatting, do this
return $extracted_plaintext;

# if you want to see your paragraphs in a web page, do this
//echo nl2br($extracted_plaintext);

}

}
}

$file1 = "japanese.doc";
$filename = "$file1";
echo $contentDoc = getPlainTextFromDocFile($filename);

//$fileLocation = "myfile.txt";
//$file = fopen($fileLocation,"w");
//fwrite($file,$content);
//fclose($file);
?>

1 个答案:

答案 0 :(得分:0)

您需要将页面的编码设置为UTF-8。

这一行应该是PHP文件的第一行。

header('Content-Type: text/html; charset=utf-8');