Forum


Replies: 1   Views: 2552
How do i use replacevariablebytext in header with firstmatch option
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 kelthalorn  · 07-11-2017 - 16:09

Hi,

We use Premium version and encounter some issue.

We have a template file with variables in header. We use replaceVariableByText with target on "header". But, we try to only set the first page instance of a variable and not on all pages. We try to add "firstMatch" true but it doesn't work.

Did we make something wrong or is this option restricted for document target (it works for this one). Have you got any idea to make this possible.

Here is log we get by dumping value during process :

  • This one with document target and a variable with 2 instance in template:
string(50) "//w:t[text()[contains(., "$prenomNomDiffuseur$")]]"
bool(false)
array(2) {
  [0]=>
  object(SimpleXMLElement)#343 (1) {
    [0]=>
    string(20) "$prenomNomDiffuseur$"
  }
  [1]=>
  object(SimpleXMLElement)#560 (1) {
    [0]=>
    string(20) "$prenomNomDiffuseur$"
  }
}
  • Now with header target and a variable instanciate:
string(47) "//w:t[text()[contains(., "$headerNomAgence$")]]"
bool(false)
array(1) {
  [0]=>
  object(SimpleXMLElement)#347 (1) {
    [0]=>
    string(17) "$headerNomAgence$"
  }
}

Thanks for your time and help

Posted by admin  · 08-11-2017 - 07:47

Hello,

UPDATE: phpdocx 9.5 added support for headers and footers in DOCXPath (https://www.phpdocx.com/documentation/introduction/docxpath), so replacements and other workflows can be done based on the header/footer position and other queries not supported by replaceVariableByText.

The replaceVariableByText method allows to use the firstMatch option with all targets. But the main document is a single XML, and the number of headers may vary. If you set firstMatch as true and the target to header (or footer), the first placeholder of each header (or footer) will be replaced.

The best approach to accomplish your task is using unique placeholders for each header (first, even, odd or per section).

Regards.