added clone, delete, replace block #165
Because I needed to clone, delete and replace some tables, I added those functions.
This commit is contained in:
parent
db57346c47
commit
e800d96cf9
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
include_once 'Sample_Header.php';
|
||||||
|
|
||||||
|
// New Word document
|
||||||
|
echo date('H:i:s') , " Create new PhpWord object" , \EOL;
|
||||||
|
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||||
|
|
||||||
|
$document = $phpWord->loadTemplate('resources/Sample_20_TemplateBlockClone.docx');
|
||||||
|
|
||||||
|
// Will clone everything between ${tag} and ${/tag}, the number of times. By default, 1.
|
||||||
|
$document->cloneBlock('CLONEME', 3);
|
||||||
|
|
||||||
|
$name = 'Sample_20_TemplateBlockClone.docx';
|
||||||
|
echo date('H:i:s'), " Write to Word2007 format", EOL;
|
||||||
|
$document->saveAs($name);
|
||||||
|
rename($name, "results/{$name}");
|
||||||
|
|
||||||
|
include_once 'Sample_Footer.php';
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
include_once 'Sample_Header.php';
|
||||||
|
|
||||||
|
// New Word document
|
||||||
|
echo date('H:i:s') , " Create new PhpWord object" , \EOL;
|
||||||
|
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||||
|
|
||||||
|
$document = $phpWord->loadTemplate('resources/Sample_21_TemplateBlockDelete.docx');
|
||||||
|
|
||||||
|
// Everything between ${tag} and ${/tag}, will be deleted/erased.
|
||||||
|
$document->deleteTemplateBlock('DELETEME');
|
||||||
|
|
||||||
|
$name = 'Sample_21_TemplateBlockDelete.docx';
|
||||||
|
echo date('H:i:s'), " Write to Word2007 format", EOL;
|
||||||
|
$document->saveAs($name);
|
||||||
|
rename($name, "results/{$name}");
|
||||||
|
|
||||||
|
include_once 'Sample_Footer.php';
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -257,6 +257,70 @@ class Template
|
||||||
$this->_documentXML = $result;
|
$this->_documentXML = $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clone a block
|
||||||
|
*
|
||||||
|
* @param mixed $blockname
|
||||||
|
* @param int $clones
|
||||||
|
* @param bool $replace
|
||||||
|
* @return null
|
||||||
|
*/
|
||||||
|
public function cloneBlock($blockname, $clones = 1, $replace = true)
|
||||||
|
{
|
||||||
|
$xmlBlock = null;
|
||||||
|
preg_match('/(<\?xml.*)(<w:p.*>\${'.$blockname.'}<\/w:.*?p>)(.*)(<w:p.*\${\/'.$blockname.'}<\/w:.*?p>)/is', $this->_documentXML, $matchs);
|
||||||
|
|
||||||
|
if (isset($matchs[3])) {
|
||||||
|
$xmlBlock = $matchs[3];
|
||||||
|
$cloned = array();
|
||||||
|
for($i = 1; $i <= $clones; $i++) {
|
||||||
|
$cloned[] = $xmlBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($replace) {
|
||||||
|
$this->_documentXML = str_replace($matchs[2].$matchs[3].$matchs[4], implode('', $cloned), $this->_documentXML);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $xmlBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace a block
|
||||||
|
*
|
||||||
|
* @param mixed $blockname
|
||||||
|
* @param $replacement
|
||||||
|
*/
|
||||||
|
public function replaceBlock($blockname, $replacement)
|
||||||
|
{
|
||||||
|
$this->deleteTemplateBlock($blockname, $replacement);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function xmlpretty($xml)
|
||||||
|
{
|
||||||
|
$domxml = new DOMDocument('1.0');
|
||||||
|
$domxml->preserveWhiteSpace = false;
|
||||||
|
$domxml->formatOutput = true;
|
||||||
|
$domxml->loadXML($xml);
|
||||||
|
$xml_string = $domxml->saveXML();
|
||||||
|
return $xml_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a block of text
|
||||||
|
*
|
||||||
|
* @param mixed $blockname
|
||||||
|
* @param string $replacement
|
||||||
|
*/
|
||||||
|
public function deleteTemplateBlock($blockname, $replacement = '')
|
||||||
|
{
|
||||||
|
$xmlBlock = null;
|
||||||
|
preg_match('/(<\?xml.*)(<w:p.*>\${'.$blockname.'}<\/w:.*?p>)(.*)(<w:p.*\${\/'.$blockname.'}<\/w:.*?p>)/is', $this->_documentXML, $matchs);
|
||||||
|
|
||||||
|
if (isset($matchs[3])) {
|
||||||
|
$this->_documentXML = str_replace($matchs[2].$matchs[3].$matchs[4], $replacement, $this->_documentXML);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save XML to temporary file
|
* Save XML to temporary file
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue