Replaces a single variable by HTML.

public replaceVariableByHTML (string $var, string $type, string $html [, array $options])

This method allows the insertion of HTML into a template. You may insert it as a "block", e.g. removing the corresponding paragraph and substituting it by the block of WordML, or "inline", e.g. just replacing the variable by inline WordML.

Practically all HTML tags and CSS styles are supported.

This method transforms HTML directly into WordML and it is compatible with OpenOffice and PDF conversion.

You may find a more detailed explanation of this useful method in the HTML to Word section of the API documentation.

To embed accents and other non-ASCII characters such as á or ü, please check that the PHP Tidy extension and the PHP mbstring extension are installed and enabled.

To embed symbols such as ∞ or Ω, please check that the PHP mbstring extension is installed and enabled.

To download images from remote URLs, please check that allow_url_fopen is enabled in the PHP config. If the remote images use HTTPS, please check that PHP is using a valid certification file and the SSL configuration is correct: More information.



The template variable to be replaced by the HTML content.


The substitution type, it can be:

  • "block": the variable, together with its containing paragraph, is replaced by the HTML content.
  • "inline": only the variable is replaced by the inline HTML content.


The HTML code to be translated into WordML: it can be a string or the path to a file.


The possible keys and values are:

Key Type Description
addDefaultStyles bool True as default, if false prevents adding default styles when strictWordStyles is false.
baseURL string The base URL used to complete the relative paths of links and images.
cssEntityDecode bool If true uses html_entity_decode to parse CSS, useful when using font families with non-ASCII names.
customListStyles bool If true checks if there is a custom list style with that name and uses it.
disableWrapValue bool If true disable using a wrap value with Tidy to prevent extra blank spaces. Default as false.
downloadImages bool If true inserts the images into the DOCX document, otherwise just links them as an external source.
embedFonts bool If true downloads and embeds TTF fonts from @font-face rules. False as default. Only available in Premium licenses.
filter string Only renders the filtered contents. It could be an string denoting an id ('#myId'), a class ('.myClass'), a HTML tag or a valid XPath expression ('//expression').
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.
forceNotTidy bool False as default. If true, avoid using Tidy. Only recommended if Tidy can't be installed.
generateCustomListStyles bool If true generates automatically the custom list styles from the list styles (decimal, lower-alpha, lower-latin, lower-roman, upper-alpha, upper-latin, upper-roman).
isFile bool True for files and false for strings.
parseAnchors bool If true parses the anchors included in the HTML code.
parseDivs string paragraph or table. By default the method parses the div elements as containers.
parseFloats bool If true preserves the floating properties of images and tables.
removeLineBreaks bool If true removes extra line breaks that can be generated when transforming HTML that includes strings without a parent tag.
strictWordStyles bool If true ignores almost all CSS styles and uses the styles set via the wordStyles option (see next).
stylesReplacementType string

Empty as default.

  • usePlaceholderStyles: keeps placeholder styles, styles from the imported HTML are ignored. Applies to the following styles: pPr, rPr.
  • mixPlaceholderStyles: mixes placeholder styles, placeholder styles overwrite HTML 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.
useHTMLExtended bool If true allows using HTML Extended tags. False as default. Only available in Premium licenses.
wordStyles array One may associate different Word styles to HTML classes, ids or tags.
For example:
  • array(".myClass" => "LightGridPHPDOCX", "#myId" => "LightListAccent6PHPDOCX", "" => "PHPDOCXHeading").

PHP Tidy is not available and forceNotTidy is false.

Code samples

Example #1

The resulting Word document looks like: