Replaces existing contents by a WordFragment.

public replaceWordContent (WordFragment $wordFragment, array $referenceNode, [string $location, bool $forceAppend])


WordFragment to be inserted.


An array to get the refence node.
The possible keys and values of this array are:

Key Type Description
type string * (all, default value), break, chart, endnote (content reference), footnote (content reference), image, paragraph (also for bookmarks, links and lists), section, shape, table, table-row, table-cell, table-cell-paragraph.
contains string Contains a text string.
occurrence int or string Exact occurrence (int) or range of contents (e.g.: 2..9, 2.., ..9) (string) or first() or last(). If empty iterate all elements.
attributes array Contains a specific attribute key and value.
parent string Main document body as default, allows to set any parent or a specific one. w:body (default), '/' (any parent) or any other specific parent (/w:tbl/, /w:tc/, /w:r/...).
target string Target content: document (default), header, footer.
reference array To be used with header and footer targets: positions (array) 1, 2... based on the sectPr contents order, sections (array) 1, 2..., types (array) first, even, default
customQuery string Custom xpath query, overwrites any other reference.


after (default), before, inlineBefore or inlineAfter. Inline locations don't create a new w:p and add the WordFragment before or after the referenceNode (only inline elements).


If true appends the WordFragment if the reference node could not be found (false as default).


Method not available.

Adding a wrong content.

Not valid location.

Code samples

Example #1

Example #2

Release notes
  • phpdocx 15.0:
    • table-row, table-cell and table-cell-paragraph types.
  • phpdocx 12.0:
    • improved working with multiple sections that contain multiple headers and footers.
  • phpdocx 9.5:
    • header and footer targets.
    • after, before, inlineBefore and inlineAfter locations.
  • phpdocx 7.0:
    • range of elements, iterate all elements not only the first one, siblings.
  • phpdocx 6.0:
    • new method.