searchAndReplace

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

PRO+ and CORPORATE PHPDocX >= 2.5

Description

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

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

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 that we want to replace.

replace

The string of text that we want to include.

options

The posible keys and values are:

keyTypeDescription
document bool If true searchs and replaces the string in the body of the document. The default values is true.
endnotes bool If true searchs and replaces the string in the document endnotes. The default values is false.
comments bool If true searchs and replaces the string in the document comments. The default values is false.
headersAndFooters bool If true searchs and replaces the string in headers and footers. The default values is true.
footnotes bool If true searchs and replaces the string in the document footnotes. The default values is false.

Return values

Void.

Code samples

Example #1: searches and replaces a given string of text


require_once 'path_to_phpdocx/classes/DocxUtilities.inc';
$newDocx = new DocxUtilities();
$options = array( 'document' => true,
		        'endnotes' => true,
		        'comments' => true,
		        'headersAndFooters' => true,
		        'footnotes' => true);

$newDocx->searchAndReplace('../files/second.docx', '../docx/example_replacedDocx.docx', 'data', 'required data', $options)

The resulting Word document looks like (download .docx file):

Change log

  • Available since 2.5