简体   繁体   English

如何从php文件中获取内容

[英]how to get content from Doc file in php

how to get content from Doc file in php 如何从php文件中获取内容

i use PHPOffice/PHPWord in Yii2. 我在Yii2中使用PHPOffice / PHPWord。

this Code Dont work: (my Doc File is UTF8) 此代码不起作用:(我的文档文件是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,...) 我需要从文档文件获取内容(标题,...)

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. .doc文件的所有文本都在[text]属性内,该属性嵌套在PHPWord加载.doc文件时创建的对象的相应[elements]属性内。 Search for them in the object you get after running $phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($filename, 'MsDoc'); 在运行$phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($filename, 'MsDoc');后得到的对象中搜索它们$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. 您将有自己的地位首先转换到public在PHPWord库文件-为[elements]这是AbstractContainer.php,并为[text]这是Text.php。 Once you have changed the status of these two properties to public , you can afterwards extract/access them from your $phpWord object. 一旦将这两个属性的状态更改为public ,就可以从$phpWord对象中提取/访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM