how to search text in DOCX file and replace it in php

The basic question is how to get docx file search any text from it then replace it with user parameters like [FIRST_NAME] using php code

 

First of all docx file is not simple text file it is a compessed file using zip.

So to parse it we are first to extract this zip file then get document.xml file from it to get more information about the file.

We have to include one zip extrator class file to extract the zip archive. so that we get the neccessary information from it.

<?php    //index.php

//include unzip class file which have all the functions to get extract zip file and get read content from docx file.

$zip = new clsTbsZip();

// Open the document
$zip->Open(‘sample.docx’);
$content = $zip->FileRead(‘word/document.xml’);

$p = strpos($content, ‘[FIRST_NAME]’);
$content = substr_replace($content, ‘Test Name’, $p, 12);
$zip->FileReplace(‘word/document.xml’, $content, TBSZIP_STRING);
$file = ‘new.docx’;
$zip->Flush(TBSZIP_FILE, $file);

?>

 

when you run the index file you have found the new.docx new generated file from old one sample.docx file, where you have found the parameter [FIRST_NAME] get changed to Test Name