Hello, I'm using a PHP library to generate a few words with some parameters.
The library is PHPWord, when I load a file (DOCX) it opens the document.xml file inside the compressed DOCX file.
Example:
<?php
$phpWord = new PHPWord('teste.docx');
$phpWord->setValue('oi', 'tudo bem');
The problem is that word XML should look like this:
<w:t>${oi}</w:t>
But it's getting like this (in some cases):
${</w:t></w:r><w:bookmarkStart w:id="1" w:name="OLE_LINK1"/><w:r w:rsidRPr="00036D2C"><w:t>oi</w:t></w:r><w:bookmarkEnd w:id="1"/><w:r w:rsidRPr="00036D2C"><w:t>}</w:t>
Because PHPWord setValue does a replace on the loaded string
/**
* 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);
}
$this->_documentXML = str_replace($search, $replace, $this->_documentXML);
}
How could I fix this using regular expression? Thank you.