使用PHPOffice/PHPWord實(shí)現(xiàn)讀取Word內(nèi)容
一:phpoffice/phpword安裝
composer require phpoffice/phpword
phpword的GitHub地址:https://github.com/PHPOffice/PHPWord
phpword文檔地址:https://phpword.readthedocs.io/en/latest/
二:加載word文檔
$word = \PhpOffice\PhpWord\IOFactory::load("xxx")
三:獲取word所有節(jié)點(diǎn)
$sections = $word->getSections()
四:獲取word所有段落
$section->getElements()
五:判斷文本元素類型
if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) { //文本元素 } else if ($element instanceof \PhpOffice\PhpWord\Element\Table) { //表格元素 }
六:獲取word文本內(nèi)容
$node->getText()
七:獲取word圖片
//獲取圖片編碼 $imageData = $node->getImageStringData(true); //添加圖片html顯示標(biāo)頭 $imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData; 八:讀取word內(nèi)容示例 /** * 獲取word文檔內(nèi)容 * @param string $wordPath * @return array */ public function getWord($wordPath = '') { //加載word文檔,使用phpword處理 $word = \PhpOffice\PhpWord\IOFactory::load($wordPath); return $this->getNodeContent($word); } /** * 根據(jù)word主節(jié)點(diǎn)獲取分節(jié)點(diǎn)內(nèi)容 * @param $word * @return array */ public function getNodeContent($word) { $return = []; //分解部分 foreach ($word->getSections() as $section) { if ($section instanceof \PhpOffice\PhpWord\Element\Section) { //分解元素 foreach ($section->getElements() as $element) { //文本元素 if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) { $text = ''; foreach ($element->getElements() as $ele) { $text .= $this->getTextNode($ele); } $return[] = $text; } //表格元素 else if ($element instanceof \PhpOffice\PhpWord\Element\Table) { foreach ($element->getRows() as $ele) { $return[] = $this->getTableNode($ele); } } } } } return $return; } /** * 獲取文檔節(jié)點(diǎn)內(nèi)容 * @param $node * @return string */ public function getTextNode($node) { $return = ''; //處理文本 if ($node instanceof \PhpOffice\PhpWord\Element\Text) { $return .= $node->getText(); } //處理圖片 else if ($node instanceof \PhpOffice\PhpWord\Element\Image) { $return .= $this->pic2text($node); } //處理文本元素 else if ($node instanceof \PhpOffice\PhpWord\Element\TextRun) { foreach ($node->getElements() as $ele) { $return .= $this->getTextNode($ele); } } return $return; } /** * 獲取表格節(jié)點(diǎn)內(nèi)容 * @param $node * @return string */ public function getTableNode($node) { $return = ''; //處理行 if ($node instanceof \PhpOffice\PhpWord\Element\Row) { foreach ($node->getCells() as $ele) { $return .= $this->getTableNode($ele); } } //處理列 else if ($node instanceof \PhpOffice\PhpWord\Element\Cell) { foreach ($node->getElements() as $ele) { $return .= $this->getTextNode($ele); } } return $return; } /** * 處理word文檔中base64格式圖片 * @param $node * @return string */ public function pic2text($node) { //獲取圖片編碼 $imageData = $node->getImageStringData(true); //添加圖片html顯示標(biāo)頭 $imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData; $return = '<img src="'.$imageData.'">'; return $return; }
調(diào)用方法:
$docx = 'XXX.docx'; $word = $this->getWord($docx);
到此這篇關(guān)于使用PHPOffice/PHPWord實(shí)現(xiàn)讀取Word內(nèi)容的文章就介紹到這了,更多相關(guān)PHPOffice PHPWord讀取Word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP number_format() 函數(shù)定義和用法
number_format() 函數(shù)通過千位分組來格式化數(shù)字2012-06-06PHP實(shí)現(xiàn)隨機(jī)發(fā)放撲克牌
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)隨機(jī)發(fā)放撲克牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04解析php addslashes()與addclashes()函數(shù)的區(qū)別和比較
本篇文章是對(duì)php中的addslashes()與addclashes()函數(shù)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP正則匹配日期和時(shí)間(時(shí)間戳轉(zhuǎn)換)的實(shí)例代碼
本文介紹下,用php實(shí)現(xiàn)正則匹配日期與時(shí)間,并進(jìn)行時(shí)間戳轉(zhuǎn)換的例子,有需要的朋友,參考下吧2016-12-12php安裝php_rar擴(kuò)展實(shí)現(xiàn)rar文件讀取和解壓的方法
這篇文章主要介紹了php安裝php_rar擴(kuò)展實(shí)現(xiàn)rar文件讀取和解壓的方法,涉及php擴(kuò)展組件的安裝與使用相關(guān)操作技巧,需要的朋友可以參考下2016-11-11標(biāo)準(zhǔn)版Eclipse搭建PHP環(huán)境的詳細(xì)步驟
這篇文章主要介紹了Eclipse搭建PHP環(huán)境的詳細(xì)步驟,感興趣的小伙伴們可以參考一下2015-11-11解析php中array_merge與array+array的區(qū)別
本篇文章是對(duì)php中array_merge與array+array的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06