【问题标题】:parsing of .doc file in php在 php 中解析 .doc 文件
【发布时间】:2014-07-01 06:34:03
【问题描述】:

我使用 html 标签创建了 .doc 文件,其中包含 HTML 表单元素,如文本框、复选框、单选按钮、下拉列表和隐藏字段。 这些在打开文档时显示正确。

  1. 当使用 php 代码更新 .doc 文件时,我能够解析 .doc 文件。并且可以在保存到数据库时使用表单文件数据。
  2. 在 .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 文件无法解析文本。

标签: php html doc


【解决方案1】:

我使用 html 标签创建了 .doc 文件

不,您创建了一个 HTML 文件并给它一个以 .doc 结尾的文件名

当您从 MSWord 保存文件时,它使用专有格式(实际上是多个嵌套格式),而不是 HTML。当您加载您最初创建的文件时,MSWord 会识别它的 HTML 并即时翻译它。有很多方法可以解决这个问题,但在充分利用它们之前,您还有很长的路要走。

您现在最好的做法是考虑为什么需要同时处理 MSWord 和 PHP 文件以及您可能使用的其他格式的问题。

【讨论】:

  • 这里我的要求是在 .doc 文件中创建离线表单。所以我需要将 html 表单元素数据保存到数据库中。这就是我创建带有 html 标签的 .doc 文件的原因。
  • 不回答这个问题 - 为什么它必须是 .doc 格式。
【解决方案2】:

如前所述,您不能像尝试那样简单地打开 Office 文件。

这是一个由 Microsoft 提供的简单易用的库,可让您做您想做的事情:

http://phpword.codeplex.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2017-04-18
    • 2012-12-22
    • 2019-01-11
    相关资源
    最近更新 更多