_tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx'; $this->_tempFileName = $name.'.docx'; copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File $this->_objZip = new ZipArchive(); $this->_objZip->open($this->_tempFileName); //Get xml $this->_documentXML = $this->_objZip->getFromName('word/document.xml'); preg_match_all('/\$\{.+?\}/', $this->_documentXML, $matches, PREG_SET_ORDER); foreach ($matches as $val) { $oldval = $val[0]; $newval = preg_replace('/\<.+?\>/', '', $val[0]); $newval = str_replace(' ', '', $newval ); $this->_documentXML = str_replace($oldval, $newval, $this->_documentXML); } //Get code between body tag $this->_documentXMLSEQ = $this->getTextBetweenTags($this->_documentXML, 'w:body'); //Get code outside tag and place string to replace later $count = null; $this->_documentXMLREM = preg_replace('/(.*?)<\/w:body>/i', '[xmlremain]', $this->_documentXML, -1, $count); //Set code as string to replace $this->_documentXML = $this->_documentXMLSEQ; unlink($name); } public function Xmlload($strFilename) { $zip = new ZipArchive(); $zip->open($strFilename); //Get xml $this->_documentAddXML = $zip->getFromName('word/document.xml'); preg_match_all('/\$\{.+?\}/', $this->_documentAddXML, $matches, PREG_SET_ORDER); foreach ($matches as $val) { $oldval = $val[0]; $newval = preg_replace('/\<.+?\>/', '', $val[0]); $newval = str_replace(' ', '', $newval ); $this->_documentAddXML = str_replace($oldval, $newval, $this->_documentAddXML); } //Get code between body tag $this->_documentXMLAddSEQ = $this->getTextBetweenTags($this->_documentAddXML, 'w:body'); $zip->close(); return $this->_documentXMLAddSEQ; } public function getTextBetweenTags($string, $tagname) { $pattern = "/<$tagname>(.*?)<\/$tagname>/"; preg_match($pattern, $string, $matches); return $matches[1]; } /** * Set a Template value * * @param mixed $search * @param mixed $replace */ public function setValue($search, $replace) { if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { $search = '${'.$search.'}'; } /* if(!is_array($replace)) { $replace = utf8_encode($replace); } */ //$newstring = implode ( $newarray ); $this->_documentXML = str_replace($search, $replace, $this->_documentXML); } public function AddPage($str) { //Add converted xml $this->_documentXMLFINAL .= $this->_documentXML; //Reset doc xml $this->_documentXML = $str; } /** * Save Template * * @param string $strFilename */ public function save($strFilename) { if(file_exists($strFilename)) { unlink($strFilename); } $this->_documentXMLFINAL .= $this->_documentXML; $this->_documentXML = str_replace('[xmlremain]', ''.$this->_documentXMLFINAL.'', $this->_documentXMLREM); $this->_objZip->addFromString('word/document.xml', $this->_documentXML); // Close zip file if($this->_objZip->close() === false) { throw new Exception('Could not close zip file.'); } rename($this->_tempFileName, $strFilename); } }