使用PHP LINUX计算DOC和DOCX中的字符数

时间:2015-02-10 20:10:43

标签: php libreoffice centos6.5

此外: 我发现最接近的计数行的方法是使用linux命令“antiword”作为DOC文件,antiword将返回DOC的文本版本;而对于DOCX使用调用将从DOCX中检索内容并通过与反词相同的文本功能推送数据。

问题来了,当你在文件中有表格时,antiword会添加很多空格。

===

我有一个脚本可以解决DOCX文件中的字符数:

$zip = new ZipArchive;


$striped_content = '';
$content = '';

if(!$filename || !file_exists($filename)) return false;

$zip = zip_open($filename);

if (!$zip || is_numeric($zip)) return false;

while ($zip_entry = zip_read($zip)) {

    if (zip_entry_open($zip, $zip_entry) == FALSE) continue;

    if (zip_entry_name($zip_entry) != "word/document.xml") continue;

    $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

    zip_entry_close($zip_entry);
}// end while

zip_close($zip_entry);

$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "\r\n", $content);
$striped_content = trim(strip_tags($content));

如果我有doc文件,我基本上使用LibreOffice命令行将文件转换为docx,而不是运行上面的脚本。

问题是我无法找到“HEADER”和“FOOTER”区域内有多少单词文件。如何实现这一目标?

我的服务器运行: PHP 5.3 LibreOffice的 CentOS 6.5

我不确定我需要提供哪些其他信息, 谢谢你的答案。

P.S。

我尝试将doc和docx转换为txt,但结果是“HEADER”和“FOOTER”区域未保存在txt文档中

此外,我找到的最接近的解决方案是: https://github.com/nagilum/DOCx

库分解整个docx文件,你有纯文本的标题,内容和页脚,我可以尝试从他们的锻炼字数。但是,libreoffice有时会严重将文件转换为docx,转换后,docx中有1页的doc文件可能有2页。

1 个答案:

答案 0 :(得分:0)

任何* .docx文件 - zip存档。它由app.xml文件组成,您可以在其中找到节点:

<Characters>8657</Characters>

并通过正则表达式提取值