Open shangdev opened 11 months ago
@shangdev Could you send a sample document for analysis ? Thanks
@Progi1984
Sample document: testDocx.docx
Sample code:
$list = [];
$source = \PhpOffice\PhpWord\IOFactory::load($filepath)->getSections();
foreach ($source as $s) {
$elements = $s->getElements();
foreach ($elements as $element) {
$class = get_class($element);
$elname = explode("\\", $class)[3];
if ($elname === 'ListItemRun') {
$textArr = getTextElement($element);
if ($textArr) {
$list[] = implode('', $textArr);
}
}
}
}
function getTextElement($element) {
$elements = $element->getElements();
if ($elements) {
$inResult = [];
foreach ($elements as $inE) {
$ns = get_class($inE);
$elName = explode('\\', $ns)[3];
if ($elName == 'Text') {
$result[] = $inE->getText();
} elseif (method_exists($inE, 'getElements')) {
$inResult = getTextElement($inE);
}
if (!is_null($inResult)) {
$result = array_merge($result, $inResult);
}
}
}
return count($result) > 0 ? $result : null;
}
echo $list;
Output: [ '标题三', '标题四', '标题五', ... ]
As shown above, when I read a word document, I can't get the serial number in front of the ListItemRun text block, e.g. 1,2,3 or one, two, three.
Is there any way to get it?