如何在Linux服务器上刮取MS Word文档文本?

时间:2010-11-24 10:45:37

标签: php linux ms-word

我被问及创建一个网站,其中一些用户可以上传Microsoft Word文档,然后其他人可以搜索包含某些关键字的上传文档。该站点将位于运行PHP和MySQL的Linux服务器上。我目前正试图找出是否以及如何从文档中删除此文本。如果有人能提出一个好的方法来做这件事,那将非常感激。

2 个答案:

答案 0 :(得分:4)

从新的docx格式中删除文本是微不足道的。文件本身只是一个zip文件,如果你查看一个文件,你会发现一堆xml文件。该文本包含在此zip文件中的word / document.xml中,所有用户输入的实际文本都将显示在< w:t>中。标签。如果您提取出现在< w:t>中的所有文字标签,你将删除文件。

答案 1 :(得分:2)

以下是使用catdoc的一个很好的例子:

function catdoc_string($str)
{
    // requires catdoc

    // write to temp file
    $tmpfname = tempnam ('/tmp','doc');
    $handle = fopen($tmpfname,'w');
    fwrite($handle,$a);
    fclose($handle);

    // run catdoc
    $ret = shell_exec('catdoc -ab '.escapeshellarg($tmpfname) .' 2>&1');

    // remove temp file
    unlink($tmpfname);

    if (preg_match('/^sh: line 1: catdoc/i',$ret)) {
        return false;
    }

    return trim($ret);
}

function catdoc_file($fname)
{
    // requires catdoc

    // run catdoc
    $ret = shell_exec('catdoc -ab '.escapeshellarg($fname) .' 2>&1');

    if (preg_match('/^sh: line 1: catdoc/i',$ret)) {
        return false;
    }

    return trim($ret);
}

Source