I want to search for this pattern:
{{{variable}}}
All the documen, so I can extrat the fileds declares as variables by the user.
IS there any mehtod to search with regular expresision based pattern?
I want to search for this pattern:
{{{variable}}}
All the documen, so I can extrat the fileds declares as variables by the user.
IS there any mehtod to search with regular expresision based pattern?
Hello,
You can use DOCXPath with the customQuery option to do custom searches through Xpath, but it doesn't extract the whole XML but its text contents using this method:
https://www.phpdocx.com/api-documentation/docx-path/get-text-word-contents-in-docx
Regards.
I made a function to do exactly what I want... How do I submit it?
At a docx document I create my "variables" like {{{ some_data }}}
I wanted to extract all the {{{ }}} from document to create an inventory, and automate the cycle of creating doc->filing->generating pdf.
I made the function and is working.. How do I submit it?
Hello,
If you think it may be useful for other users and it's not too long, you can post the function in this topic.
Regards.
at DocxUtilities.inc
Usage:
for a pattern of {{{variable_name}}} inside document:
$matches=$newDocx->rawRegExpSearch('file_test.docx', '|{{{([a-zA-Z0-9_]+)}}}|');
and it ouptputs:
[0].-{{{contract_number}}}
[1].-{{{colaterals}}}
[2].-{{{customer_name}}}
Then I create a html form to fill in the values and generate a new document.
/**
* Search and replace text in any XML file of the document. Raw replacement, use carefully
*
* @access public
* @param string $document path to the document
* @param string $regExp regular expression to match the regex
* @return array of the value matches
*/
public function rawRegExpSearch($document, $regExp)
{
$docxFile = new ZipArchive();
// $finalDocument="finalDocumentRawRegExp".uniqid();
$matches=array();
// make a copy of the the document into its final destination to don't overwrite it
// copy($document, $finalDocument);
// extract (some) of the relevant files of the copy of the first document for manipulation
$docxFile->open($document);
$docxNumFiles = $docxFile->numFiles;
// iterate the ZIP content and replace strings in the XML files
for ($i = 0; $i < $docxNumFiles; $i++) {
$fileName = $docxFile->getNameIndex($i);
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
if ($fileExtension == 'xml' || $fileExtension == 'rels') {
// replace the string and save it in the ZIP file
$fileContent = $docxFile->getFromName($fileName);
$out=array();
preg_match_all($regExp, $fileContent, $out);
if(sizeof($out[0]) > 1)
array_push($matches, $out[0]);
}
}
$docxFile->close();
$final=array();
foreach($matches as $idx => $m)
if(sizeof($m)>0)
{
foreach($m as $k => $v)
$final[]=$v;
}
else
$final[]=$m;
return $final;
}