-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpword2text.php
More file actions
66 lines (56 loc) · 1.55 KB
/
Copy pathphpword2text.php
File metadata and controls
66 lines (56 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class PHPWord2Text {
protected function getElementsText($elements, $finish_char=' '){
$retText = '';
foreach($elements as $el){
if(method_exists($el,'getText')){
$retText .= $el->getText().$finish_char;
}
elseif($el instanceof \PhpOffice\PhpWord\Element\Table){
$rows = $el->getRows();
foreach($rows as $r){
$cells = $r->getCells();
foreach($cells as $c){
$retText .= $this->getElementsText($c->getElements())." ";
}
}
}
else{
if(method_exists($el,'getElements')){
if(get_class($el) == 'PhpOffice\PhpWord\Element\TextRun'){
$retText .= getElementsText($el->getElements(),'')." ";
}
else{
$retText .= getElementsText($el->getElements())." ";
}
}
}
}
return $retText;
}
public function extractText($filename){
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if($ext == 'docx'){
$phpWord = \PhpOffice\PhpWord\IOFactory::load($filename, 'Word2007'); //for .docx files
}
elseif($ext == 'doc'){
$phpWord = \PhpOffice\PhpWord\IOFactory::load($filename, 'MsDoc'); //for .doc files
}
elseif($ext == 'rtf'){
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source,'RTF'); //for RTF files
}
elseif($ext == 'odt'){
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'ODText'); //for .odt files
}
else{
return ''; //nothing to do here
}
$allSections = $phpWord->getSections();
$ret = '';
foreach($allSections as $s){
$allElements = $s->getElements();
$ret .= $this->getElementsText($allElements);
}
return $ret;
}
}