我使用html标签创建了。doc文件,它具有html表单元素,如文本框,复选框,单选按钮,下拉框和隐藏字段。当打开文档时,这些显示正常。
- 当用php代码更新。doc文件时,我能够解析。doc文件。并且能够在保存到数据库时使用表单字段的数据。
- 当在。doc文件中使用'另存为'选项时,新创建的doc文件正确显示html表单元素。但是无法解析"另存为"文件中的数据。
我想解析'另存为' doc文件也使用php。请帮助我如何解决这个问题?
这是我的doc文件解析代码:
function parseWord($userDoc)
{
$fileHandle = fopen($userDoc, "r");
$line = @fread($fileHandle, filesize($userDoc));
$lines = explode(chr(0x0D),$line);
$outtext = "";
foreach($lines as $thisline)
{
$pos = strpos($thisline, chr(0x00));
if (($pos !== FALSE)||(strlen($thisline)==0))
{
} else {
$outtext .= $thisline." ";
}
}
if(trim($outtext)==""){
$outtext ="";
//echo "<br> UTF ";
$filename = $userDoc;
if ( file_exists($filename) ) {
$outtext ="";
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
//echo $extracted_plaintext;
$outtext .= $extracted_plaintext;
# if you want to see your paragraphs in a web page, do this
//echo nl2br($extracted_plaintext);
}
fclose($fh);
}
}
$outtext = preg_replace("/[^a-zA-Z0-9's','.'-'n'r't@'/'_'(')]/","",$outtext);
return $outtext;
}
$userDoc = "cv.doc";
$text = parseWord($userDoc);
echo $text;
我使用html标签创建。doc文件
不,你创建了一个HTML文件,并给它一个以。doc
结尾的文件名当您从MSWord保存文件时,它使用专有格式(实际上是多个嵌套格式),而不是HTML。当您加载最初创建的文件时,MSWord会识别出它是HTML并动态地翻译它。有很多方法可以解决这个问题,但在你能够充分利用它们之前,你还有很长的路要走。
现在最好的做法是考虑为什么需要同时使用MSWord和PHP处理文件,以及可能使用的其他格式。
正如已经说过的,你不能简单地打开Office文件。
这是一个简单易用的库,由微软提供,它可以让你做你想做的事:
http://phpword.codeplex.com/