【问题标题】:Get word count for first line in doc/docx/pdf file获取 doc/docx/pdf 文件中第一行的字数
【发布时间】:2019-10-22 20:25:20
【问题描述】:

对于我的任务,我必须获取上传的 .doc、.docx 或 .pdf 文件的总字数。然后,我必须在文档的第一行找到字数并将其从总数中删除(因为它可能是标题)。

我正在使用doccounter 来查找文档的总字数:

include "class.doccounter.php";

$doc = new DocCounter();
$doc->setFile("file.ext");

print_r($doc->getInfo());
echo ($doc->getInfo()->wordCount);

剩下的就是找到上传文件第一行的字数。欢迎任何解决方案,包括额外的库或本机实现!谢谢!

编辑 - 解决方案(归功于Rustyjim):

$doc = new DocCounter();
$doc->setFile("file.pdf");
$text = $doc->getInfo()->toText; // Edited doccounter to return text as string
$array = explode("\n", $text); // every cell contains a new line of the text
echo $array[0]; // First line

【问题讨论】:

    标签: php php-5.6


    【解决方案1】:

    也许您可以在换行符上使用explode,例如:

    $array = explode("\n", $doc);
    

    然后使用数组的第一个元素来统计字符数:

    echo strlen($array[0]);
    

    希望有帮助

    【讨论】:

    • 谢谢,成功了!我稍微更改了 doccounter 以便能够将文本作为字符串返回,然后使用您的解决方案。
    • 太棒了!很高兴听到
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多