Skip to content

Instantly share code, notes, and snippets.

@abarke
Forked from AndrewChamp/docx_editor.php
Last active April 7, 2024 17:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save abarke/cee03d78397f7fc0ff528ce940c1680b to your computer and use it in GitHub Desktop.
Save abarke/cee03d78397f7fc0ff528ce940c1680b to your computer and use it in GitHub Desktop.
Edit a Microsoft Word .docx file using PHP and the zip extension.
<?php
$input = 'original.docx';
$output = 'modified.docx';
$replacements = [
'{test-placeholder-1}' => 'test successful 1',
'{test-placeholder-2}' => 'test successful 2',
'{test-placeholder-3}' => 'test successful 3',
'{test-placeholder-4}' => 'test successful 4',
];
$successful = searchReplaceWordDocument($input, $output, $replacements);
echo $successful ? "Successfully created $output" : 'Failed!';
/**
* Edit a Word 2007 and newer .docx file.
* Utilizes the zip extension http://php.net/manual/en/book.zip.php
* to access the document.xml file that holds the markup language for
* contents and formatting of a Word document.
*
* In this example we're replacing some token strings. Using
* the Office Open XML standard ( https://en.wikipedia.org/wiki/Office_Open_XML )
* you can add, modify, or remove content or structure of the document.
*
* @param string $input
* @param string $output
* @param array $replacements
*
* @return bool
*/
function searchReplaceWordDocument(string $input, string $output, array $replacements): bool
{
if (copy($input, $output)) {
// Create the Object.
$zip = new ZipArchive();
// Open the Microsoft Word .docx file as if it were a zip file... because it is.
if ($zip->open($output, ZipArchive::CREATE) !== true) {
return false;
}
// Fetch the document.xml file from the word subdirectory in the archive.
$xml = $zip->getFromName('word/document.xml');
// Replace
$xml = str_replace(array_keys($replacements), array_values($replacements), $xml);
// Write back to the document and close the object
if (false === $zip->addFromString('word/document.xml', $xml)) {
return false;
}
$zip->close();
return true;
}
return false;
}
@ChinigamiHunter
Copy link

Thank you for sharing, and for images there is any way to replace image Or add images to word document using xml ?

@handhikadj
Copy link

handhikadj commented Feb 1, 2021

doesn't work

@JBavaliya
Copy link

JBavaliya commented Apr 28, 2021

It's doesn't working for .doc file extenstion, can you please help for .doc file read and edit

@desarrollotec1
Copy link

Hello friends
Work with 'errors', because not replace all template tags

@dearsina
Copy link

This won't work except in the most ideal situations. The openXML format is a lot more complex:
https://stackoverflow.com/questions/34002797/use-openxml-to-replace-text-in-docx-file-strange-content

@abarke
Copy link
Author

abarke commented Sep 27, 2021

You need to make sure the placeholders are not transformed by Word. I opened the docx files using 7zip, edited the document directly where the placeholders are and fixed the formatting of the text to make sure the placeholder text is what it should be when searching and replacing.

@kamleshwebtech
Copy link

how to bold replaced text/word?

@abarke
Copy link
Author

abarke commented Dec 1, 2023

how to bold replaced text/word?

open the file in 7zip and find the xml file. Then find exactly the content you wish the replace... e.g. the text inside the bold elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment