Finding and replacing text with PHPWord

Finding and replacing text with PHPWord

PHPWord is only intented to create docx files.

Anyhow, a docx file is simply a collection of xml files in a zip container. In general what you need to do is:

  1. Rename xxx.docx to
  2. Unzip to a temporary folder
  3. In temporary_folder_from_unzipped_doc/word read in document.xml which contains all text of the document (except headers and footers)
  4. Replace what you need to replace
  5. Save the document.xml
  6. Zip the whole temporary folder back to xxx.docx

Beware that Word has a tendency to split words in multiple sections, depending on what was done with the document at save point. There may be Bookmarks, language changes, spellchecking or character formating tags in the middle of any word.

Good luck to find a safe way to handle this.


Open temporary_folder_from_unzipped_doc/word/document.xml in notepad++

Search for

And replace with