Regular expression in XML string

0

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.

    
asked by anonymous 04.02.2017 / 20:35

1 answer

0

This regex captures the non-special characters that are between the <w:t>  and <\/w:t>

(<w:t>[a-zA-Z0-9]*<\/w:t>)

It would return <w:t>oi</w:t> so you could use a replace to put ${ and } .

    
07.04.2017 / 21:08