【发布时间】:2014-07-01 06:34:03
【问题描述】:
我使用 html 标签创建了 .doc 文件,其中包含 HTML 表单元素,如文本框、复选框、单选按钮、下拉列表和隐藏字段。 这些在打开文档时显示正确。
- 当使用 php 代码更新 .doc 文件时,我能够解析 .doc 文件。并且可以在保存到数据库时使用表单文件数据。
- 在 .doc 文件中使用“另存为”选项时,新创建的 doc 文件可以正确显示 html 表单元素。但无法解析“另存为”文件中的数据。
我也想使用 php 解析“另存为”doc 文件。请帮助我如何解决这个问题?
这是我的 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;
提前谢谢...
【问题讨论】:
-
你能把你的解析代码贴出来让我们看看吗。
-
您的意思是您的 doc 文件中有一个“另存为”按钮?但是,当您在 MS Word 中打开它时,按钮不起作用?
-
当我在 MS Word 中打开并单击“另存为”按钮时。然后新的 doc 文件无法解析文本。