Forum


Replies: 1   Views: 81
Extract variables shifted

Posted by Tommy@Nsure  · 05-02-2025 - 06:07

Deleted by Tommy@Nsure · 05-02-2025 - 12:56

Posted by admin  · 05-02-2025 - 09:04

Hello,

What license and version of phpdocx are you using?

We recommend you read the following documentation pages:

Your DOCX contains the symbol used to wrap placeholders ($) a regular content (such as $$PFEE_AMT$ ). Therefore, methods that detect the variables automatically (such as getTemplateVariables) can't get them correctly.

We recommend you doing one of the following approaches:

  • Remove $ as regular content in the template. For example, write $PFEE_AMT$ instead of $$PFEE_AMT$ . When you replace the variable you can add the extra $ in the new text.
  • Use another symbol to wrap placeholders that is not used as regular content, for example # in your template.
  • Enable the parseMode option if you can't change the placeholders used in the template:
$docx = new CreateDocxFromTemplate('sample.docx', array('parseMode' => true));
CreateDocxFromTemplate::$regExprVariableParse = '(.+)';

print_r($docx->getTemplateVariables());
  • Use ${} to wrap placeholders. For example $${PFEE_AMT} (in this case you can keep $ as regular content).

We recommend you use ${} to wrap placeholders, that is the more accurate and easiest approach for your template. After updating your template using ${} you can get all template variables as needed:

$docx = new CreateDocxFromTemplate('sample.docx');
$docx->setTemplateSymbol('${', '}');

print_r($docx->getTemplateVariables());

Regards.