searchAndReplace

searchAndReplace

ADVANCED / PREMIUM TRIAL

Searches and replaces a given string of text in a Word document.

Description
public searchAndReplace (string $source, string $target, mixed $search, mixed $replace [, array $options])

This method allows the substitution of plain text in a DOCX document.

This method doesn't use the template engine included in phpdocx. We recommend using replaceVariableByText or replaceVariableByWordFragment to do text replacements.

Parameters

source

The path to the source Word document.

target

The path to the Word document resulting after the text replacement.

search

The string of text or array that you want to replace.

replace

The string of text or array that you want to include.

options

The possible keys and values are:

Key Type Description
document bool If true searches and replaces the string in the body of the document. The default value is true.
endnotes bool If true searches and replaces the string in the document endnotes. The default value is false.
comments bool If true searches and replaces the string in the document comments. The default value is false.
headersAndFooters bool If true searches and replaces the string in headers and footers. The default value is false.
footnotes bool If true searches and replaces the string in the document footnotes. The default value is false.
headers bool If true highlights the search string in headers. The default value is false.
footers bool If true highlights the search string in footers. The default value is false.
Exceptions

Error while trying to open the (base) template as a zip file.

Code samples

Example #1

The resulting Word document looks like:

Release notes
  • phpdocx 14.0:
    • return DOCXStructure.
  • phpdocx 13.5:
    • DOCXStructure and in-memory DOCX documents.
    • improved when content paragraphs to be updated start with protected XML characters or not standard double quotation marks.
  • phpdocx 13.0:
    • headers and footers scopes.
  • phpdocx 12.5:
    • array in search parameter.