The replaceVariableByText method keeps existing styles when replacing placeholders.
Using the replaceVariableByWordFragment method, If the styles are applied to the paragraph (w:pPr styles), when you do an inline replacement the existing styles remain, but if the styles are applied to the placeholder (w:rPr styles), then replacing the placeholder by a WordFragment the styles will be changed to use the styles from the WordFragment. Premium licenses include options to keep and mix existing styles when replacing a placeholder in complex replacements, for example replaceVariableByHTML includes the stylesReplacementType option:
stylesReplacementType string Empty as default. Only available in Premium licenses.
* 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.
Since phpdocx 9, Advanced and Premium licenses also include getWordStyles. This method can be used for example to get existing styles from a placeholder and apply them to the WordFragment to be added.
We recommend you to upgrade to the latest Premium version, so you can do complex replacements to mix and keep styles when replacing placeholders by HTML and WordFragments.