Forum


Replies: 1   Views: 1898
Replacevariablebytext seems to fail randomly on only one or two fields
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by cem  · 08-08-2019 - 20:41

Hello,

We have the Enterprise license for v.7 installed in a production server.  We've noticed that when a new .docx template is created, sometimes randomly a field will not take the variable data passed to it.  I have examples of this and I can usually recreate the problem.  This seems to go away if I edit the .docx and simply re-type the variable.  I can't say for sure what causes this because it does seem to change.  I can literally copy the .docx as a test version and the original will be fine, but the copy will again leave the field without putting in the data.  I've confirmed over and over that the data is present and that the fieldnames match (no typo, etc) but it definitely will fail. 

Is there some known bug or field or symbol we should avoid?  We use ^ as the symbol and I notice this happens sometimes in the footer more than other "regular" parts of the .docx.  One example that almost consistently does this is at the beginning of the line in the footer we will have ^cus_num^.  Could it be related to the ^ symbol?  The fact that it starts the line off and is numeric data?  The underscore in the field (we use underscores with no problem elsewhere)?  I can literally copy that field from the footer and place it above in the body and one spot will work and the other will not!  There is no special formatting on the text field, it is normal. 

Here is a snippet (the other data will be fine but not cus_num):

        $footvar = array(
          'acct_code'   => $data['common']['c_acct_code'],
          'prevnum'     => (!empty($data['quote']['QuotePrevNum'])) ? $data['quote']['QuotePrevNum'] : 'NEW',
          'quote_num'   => $data['quote']['QuoteNumber'],
          'eb'          => $eb,
          'cus_num'     => (!empty($data['customer']['CustomerNumber'])) ? $data['customer']['CustomerNumber'] : 'New'
        );
        $foot = array('target' => 'footer');
        $docx->replaceVariableByText($footvar, $foot);


Regards, cem

Posted by admin  · 09-08-2019 - 07:23

Hello,

There's no known bug using template methods such as replaceVariableByText and we have hundreds of unit tests of these features.

MS Word can break strings adding extra tags, but since phpdocx 3 (launched years ago), phpdocx handle them perfectly. On https://www.phpdocx.com/documentation/cookbook/improve-performance-with-templates (Replacement of variables with preprocessed documents section) you can read the full description of how this MS Word behaviour works.

Please note that phpdocx cleans that placeholders automatically unless you load the template setting the preprocessed as true:

$docx = new CreateDocxFromTemplate('template.docx', array('preprocessed' => true));

If you set this option as true (default is false) to get a faster performance, then phpdocx doesn't repair the placeholders and broken placeholders aren't remove correctly. On the previous page you can read all information.

As your license has an active LUS, have you tested the same DOCX and script using phpdocx 9? If you send to contact[at]phpdocx.com a sample of the DOCX that contains some placeholder not replaced correctly will check it.

Regards.