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
| <?php
class findInDOCX {
private $docxFileName;
private $patternStr = "/<w:t[^>]*>([^<]*)<\/w:t>/msi";
private $text;
public function load($docxFileName) {
if (!preg_match("/\.docx$/i",$docxFileName)) die("findInDOCX expects the file to be .docx");
if (!file_exists($docxFileName)) die("findInDOCX expects the file $docxFileName to exist");
$this->docxFileName = $docxFileName;
$this->text = "";
$this->exec();
}
private function exec() {
$document = 'word/document.xml';
$this->resultArray = array();
$zip = new ZipArchive;
if ($zip->open($this->docxFileName) === true) {
$workDocument = $zip->getFromName($document);
preg_match_all($this->patternStr,$workDocument,$arrText);
foreach($arrText[1] as $key => $value) {
$arrText[1][$key] = html_entity_decode($value,ENT_XML1);
}
$this->text = implode('',$arrText[1]);
//var_dump($this->text);
$zip->close();
} else {
die("findInDOCX => exec is unable to unzip ".$this->docxFileName);
}
}
public function find($searchString) {
return preg_match("/".$searchString."/msi",$this->text) == 1 ? true : false;
}
}
$a = new findInDOCX();
$a->load("xlsxExcelReader.class.php.docx");
echo $a->find('nommée') ? "trouvé" : "pas trouvé";
echo "<br/>";
echo $a->find('hike ') ? "trouvé" : "pas trouvé"; |
Partager