replaceListVariable

replaceListVariable

TRIAL / ADVANCED / PREMIUM

Replaces a single variable within a list by a list of items.

Description
public replaceListVariable (string $variable, array $listValues[, array $options])

This method is designed to generate a Word list with an initially undetermined number of items.

If a placeholder variable is included as a list item, phpdocx will clone that entry as many times as needed, populating each resulting item with the corresponding values from the $listValues array.

Parameters

variable

The name of the variable that you want to replace by a list of items.

listValues

The array with the values (text strings or WordFragments) you want to use to populate the list.

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.
target string Possible values are: document (default), header, footer. This option sets the scope of the replacement procedure.
parseLineBreaks boolean If true (default is false) parses the line breaks to include them in the Word document.
type string inline (default) or block; used by WordFragment values.
wordFragmentOptions (Available in Premium licenses) array
  • stylesReplacementType (string): usePlaceholderStyles (keep placeholder styles, styles from the imported HTML are ignored), mixPlaceholderStyles (mix placeholder styles, placeholder styles overwrite HTML styles with the same name). Applies to the following styles: pPr, rPr
  • stylesReplacementTypeIgnore (array): styles to be ignored from the imported WordFragment. Use with mixPlaceholderStyles
  • stylesReplacementTypeOverwrite (bool): if true, overwrite the placeholder styles don't set in stylesReplacementTypeIgnore. Use with mixPlaceholderStyles. Default as false
Code samples

Example #1

The resulting Word document looks like:

Release notes
  • phpdocx 17.0:
    • wordFragmentOptions option.
  • phpdocx 13.0:
    • parseLineBreaks option.
  • phpdocx 12.0:
    • supported sub-arrays.
  • phpdocx 7.0:
    • header and footer targets.
  • phpdocx 6.5:
    • supported WordFragments as values.
  • phpdocx 6.0:
    • supported literals like '\n' and carriage returns like "\n" with the parseLineBreaks option.