replaceVariableByWordFragment

replaceVariableByWordFragment

TRIAL / ADVANCED / PREMIUM

Replaces placeholder variables by fragments of Word.

Description
public replaceVariableByWordFragment (array $variables[, array $options])

This powerful method allows the substitution of placeholder variables by Word fragments that may include:

  • formatted text
  • tables
  • images
  • charts
  • footnotes
  • ...

The Word fragments should be instances of the WordFragment class.

The available WordFragment targets to be used with this method are the following: body of the document ('document'), headers ('header'), footers ('footer'), footnotes ('footnote'), endnotes ('endnote') and comments ('comment'). Specific header and footer targets such as defaultHeader or firstFooter aren't used.

Parameters

variables

This must be an array with the names of the placeholder template variables as keys and the WordFragment instances that should replace them as values.

options

The possible keys and values of this array are:

Key Type Description
firstMatch boolean If the variable appears more than once within the Word document and firstMatch is set to true, phpdocx will only replace the first occurrence of the variable within the Word document.
stylesReplacementType string

Empty as default.

  • usePlaceholderStyles: keeps placeholder styles, styles from the imported WordFragment are ignored. Applies to the following styles: pPr, rPr.
  • mixPlaceholderStyles: mixes placeholder styles, placeholder styles overwrite WordFragment styles with the same name. Applies to the following styles: pPr, rPr.

Only available in Premium licenses.

stylesReplacementTypeIgnore array

Styles to be ignored from the imported HTML. Use with mixPlaceholderStyles. Empty as default.

Only available in Premium licenses.

stylesReplacementTypeOverwrite bool

If true, HTML styles overwrite placeholder styles with the same name that are not set in the stylesReplacementTypeIgnore option. Use with mixPlaceholderStyles. Default as false.

Only available in Premium licenses.

target string Possible values are: document (default), header, footer, footnote, endnote, comment. This option sets the scope of the replacement procedure.
type string Possible values are:
  • block (default): replaces the variable and removes its containing paragraph.
  • inline: only replaces the variable by the WordFragment inline content (it may be used to replace a variable by formatted text).
  • inline-block: replaces the variable keeping inline and block elements. Only available in Premium licenses.
Exceptions

Not using WordFragments.

Code samples

Example #1

The resulting Word document looks like:

Example #2

The resulting Word document looks like: