php 提取文字,如何使用PHP从word文档中提取文本内容?

在尝试使用PHP的PhpOfficePhpWord库从Word 97-2004文档(.doc)中提取文本时,代码只能部分提取内容。经过测试,问题可能涉及到兼容性或代码本身。代码中遍历元素并打印文本,但只输出了'The quick brown fox j'。进一步的`var_dump`显示了元素的详细信息,但未找到完整文本的直接原因。
摘要由CSDN通过智能技术生成

我想用PHP从word文档中提取文本内容.

我在Microsoft Word for Mac 2011中创建了一个新的单词文档.

编辑:也通过在Windows 7中的Microsoft Word中创建相同的文档进行测试.

文件的内容是

The quick brown fox jumps over the lazy dog

我把它保存到磁盘作为Word 97-2004文档(.doc).

$source = "word.doc";

$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'MsDoc');

$text = '';

$sections = $phpWord->getSections();

foreach ($sections as $s) {

$els = $s->getElements();

foreach ($els as $e) {

if (get_class($e) === 'PhpOffice\PhpWord\Element\Text') {

$text .= $e->getText();

} elseif (get_class($e) === 'PhpOffice\PhpWord\Section\TextBreak') {

$text .= " \n";

} else {

throw new Exception('Unknown class type ' . get_class($e));

}

}

}

print $text;

此代码的输出只是文本的一部分:

The quick brown fox j

代码有问题,还是某种兼容性问题?

编辑:

如果我添加一个var_dump($els);之前($els为$e){输出是这样的:

array(1) {

[0]=>

object(PhpOffice\PhpWord\Element\Text)#1265 (14) {

["text":protected]=>

string(21) "The quick brown fox j"

["fontStyle":protected]=>

object(PhpOffice\PhpWord\Style\Font)#1267 (25) {

["aliases":protected]=>

array(1) {

["line-height"]=>

string(10) "lineHeight"

}

["type":"PhpOffice\PhpWord\Style\Font":private]=>

string(4) "text"

["name":"PhpOffice\PhpWord\Style\Font":private]=>

NULL

["hint":"PhpOffice\PhpWord\Style\Font":private]=>

NULL

["size":"PhpOffice\PhpWord\Style\Font":private]=>

NULL

["color":"PhpOffice\PhpWord\Style\Font":private]=>

NULL

["bold":"PhpOffice\PhpWord\Style\Font":private]=>

bool(false)

["italic":"PhpOffice\PhpWord\Style\Font":private]=>

bool(false)

["underline":"PhpOffice\PhpWord\Style\Font":private]=>

string(4) "none"

["superScript":"PhpOffice\PhpWord\Style\Font":private]=>

bool(false)

["subScript":"PhpOffice\PhpWord\Style\Font":private]=>

bool(false)

["strikethrough":"PhpOffice\PhpWord\Style\Font":private]=>

bool(false)

["doubleStrikethrough":"PhpOffice\PhpWord\Style\Font":private]=>

bool(false)

["smallCaps":"PhpOffice\PhpWord\Style\Font":private]=>

bool(false)

["allCaps":"PhpOffice\PhpWord\Style\Font":private]=>

bool(false)

["fgColor":"PhpOffice\PhpWord\Style\Font":private]=>

NULL

["scale":"PhpOffice\PhpWord\Style\Font":private]=>

NULL

["spacing":"PhpOffice\PhpWord\Style\Font":private]=>

NULL

["kerning":"PhpOffice\PhpWord\Style\Font":private]=>

NULL

["paragraph":"PhpOffice\PhpWord\Style\Font":private]=>

object(PhpOffice\PhpWord\Style\Paragraph)#1266 (26) {

["aliases":protected]=>

array(1) {

["line-height"]=>

string(10) "lineHeight"

}

["basedOn":"PhpOffice\PhpWord\Style\Paragraph":private]=>

string(6) "Normal"

["next":"PhpOffice\

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值