replaceVariableByHTML

replaceVariableByHTML

BASIC / ADVANCED / PREMIUM

Replaces a single variable by HTML.

Description
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.

Parameters

var

The template variable to be replaced by the HTML content.

type

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.

html

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

options

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.
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.
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 ocurrence of the variable within the Word document.
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 all CSS styles and uses the styles set via the wordStyles option (see next).
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").
Code samples

Example #1

The resulting Word document looks like: