Cookbook
- Learn phpdocx in 5 minutes
- Tutorial
- API quick guide
- HTML to Word
- HTML Extended
- Conversion plugin
- Word to HTML
- DOCXPath
- Bulk processing
- DOCXCustomizer
- Digital signature
- Cryptophpdocx
- Right to left languages
- phpdocx CLI command
- Tracking
- Artificial Intelligence
- Blockchain for documents
- JavaScript API
- Compiled mode
Replace placeholders in targets
By default, methods that replace placeholders in templates like replaceVariableByText or replacePlaceholderImage substitute the existing placeholders in the body of the document with new contents.
If you need to replace placeholders in other targets, use the target option, available in all methods. This way, it is possible to exchange placeholders in any target: document, header, footer, footnote, endnote and comment.
For example, this following code replaces a placeholder called VAR_NAME in the body of the document, as the target is not specified.
If there's a placeholder in the header that you would like to replace, just set the target with header as its value.
You may have noticed that the target option allows one single value. Replacements of placeholders in different targets require to execute the method as many times as targets are.
It is also possible to exchange a placeholder in all targets. Write this snippet to do so:
You can substitute replaceVariableByText with the replacement method of your choice: replacePlaceholderImage, replaceVariableByHTML, replaceVariableByWordFragment...