获取 doc/docx/pdf 文件中第一行的字数

Get word count for first line in doc/docx/pdf file

对于我的任务,我必须获取上传的 .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

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

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

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

echo strlen($array[0]);

希望对您有所帮助