2

how to get content from Doc file in php

i use PHPOffice/PHPWord in Yii2.

this Code Dont work: (my Doc File is UTF8)

$phpWord = \PhpOffice\PhpWord\IOFactory::load($filename, 'MsDoc');
$sections = $phpWord->getSections();
foreach ($sections as $key => $value) {
    $sectionElement = $value->getElements();
    foreach ($sectionElement as $elementKey => $elementValue) {
        if ($elementValue instanceof \PhpOffice\PhpWord\Element\TextRun) {
            $secondSectionElement = $elementValue->getElements();
            foreach ($secondSectionElement as $secondSectionElementKey => $secondSectionElementValue) {
                if ($secondSectionElementValue instanceof \PhpOffice\PhpWord\Element\Text) {
                    echo $secondSectionElementValue->getText() . '<br/>';
                }
            }
        }
    }
}

i need get content from Doc File (Title,...)

user3770797
  • 374
  • 5
  • 24

1 Answers1

0

All the text of a .doc file is inside the [text] properties nested inside corresponding [elements] properties of the object PHPWord creates when loading your .doc file. Search for them in the object you get after running $phpWord = \PhpOffice\PhpWord\IOFactory::load($filename, 'MsDoc'); in your code.

These properties by default are protected. You will have to first change their status to public in PHPWord library files - for [elements] it is AbstractContainer.php, and for [text] it is Text.php. Once you have changed the status of these two properties to public, you can afterwards extract/access them from your $phpWord object.