Replies: 2   Views: 1055
Use replacevariablebytext with multiple targets
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 rstoeber  · 06-06-2021 - 19:13

I have been using replaceVariableByText on the document body with no problem. For the first time today I have a Word document with a "variable" in the footer, and it wasn't replaced. Reading the documenation (always a good idea) I see that I have to specify the footer, or header, specifically as a target for the replacement.

The question is, can I combine multiple targets in a single statement, or do I have to have to execute replaceVariableByText 3 separate times to replace in the header, body and footer?


Posted by admin  · 06-06-2021 - 19:54


Please check the documentation available on The target option allows setting a single value, but you can replace all targets adding just two lines:

foreach (array('document', 'header', 'footer', 'footnote', 'endnote', 'comment') as $target) {
  $docx->replaceVariableByText($variables, array('target' => $target));


Posted by rstoeber  · 06-06-2021 - 20:23

Thank you. That works.