Skip to content

Commit e800d96

Browse files
author
Diego Vieira
committed
added clone, delete, replace block #165
Because I needed to clone, delete and replace some tables, I added those functions.
1 parent db57346 commit e800d96

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// New Word document
5+
echo date('H:i:s') , " Create new PhpWord object" , \EOL;
6+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
8+
$document = $phpWord->loadTemplate('resources/Sample_20_TemplateBlockClone.docx');
9+
10+
// Will clone everything between ${tag} and ${/tag}, the number of times. By default, 1.
11+
$document->cloneBlock('CLONEME', 3);
12+
13+
$name = 'Sample_20_TemplateBlockClone.docx';
14+
echo date('H:i:s'), " Write to Word2007 format", EOL;
15+
$document->saveAs($name);
16+
rename($name, "results/{$name}");
17+
18+
include_once 'Sample_Footer.php';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// New Word document
5+
echo date('H:i:s') , " Create new PhpWord object" , \EOL;
6+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
8+
$document = $phpWord->loadTemplate('resources/Sample_21_TemplateBlockDelete.docx');
9+
10+
// Everything between ${tag} and ${/tag}, will be deleted/erased.
11+
$document->deleteTemplateBlock('DELETEME');
12+
13+
$name = 'Sample_21_TemplateBlockDelete.docx';
14+
echo date('H:i:s'), " Write to Word2007 format", EOL;
15+
$document->saveAs($name);
16+
rename($name, "results/{$name}");
17+
18+
include_once 'Sample_Footer.php';
14.9 KB
Binary file not shown.
15 KB
Binary file not shown.

src/PhpWord/Template.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,70 @@ public function cloneRow($search, $numberOfClones)
257257
$this->_documentXML = $result;
258258
}
259259

260+
/**
261+
* Clone a block
262+
*
263+
* @param mixed $blockname
264+
* @param int $clones
265+
* @param bool $replace
266+
* @return null
267+
*/
268+
public function cloneBlock($blockname, $clones = 1, $replace = true)
269+
{
270+
$xmlBlock = null;
271+
preg_match('/(<\?xml.*)(<w:p.*>\${'.$blockname.'}<\/w:.*?p>)(.*)(<w:p.*\${\/'.$blockname.'}<\/w:.*?p>)/is', $this->_documentXML, $matchs);
272+
273+
if (isset($matchs[3])) {
274+
$xmlBlock = $matchs[3];
275+
$cloned = array();
276+
for($i = 1; $i <= $clones; $i++) {
277+
$cloned[] = $xmlBlock;
278+
}
279+
280+
if ($replace) {
281+
$this->_documentXML = str_replace($matchs[2].$matchs[3].$matchs[4], implode('', $cloned), $this->_documentXML);
282+
}
283+
}
284+
return $xmlBlock;
285+
}
286+
287+
/**
288+
* Replace a block
289+
*
290+
* @param mixed $blockname
291+
* @param $replacement
292+
*/
293+
public function replaceBlock($blockname, $replacement)
294+
{
295+
$this->deleteTemplateBlock($blockname, $replacement);
296+
}
297+
298+
public function xmlpretty($xml)
299+
{
300+
$domxml = new DOMDocument('1.0');
301+
$domxml->preserveWhiteSpace = false;
302+
$domxml->formatOutput = true;
303+
$domxml->loadXML($xml);
304+
$xml_string = $domxml->saveXML();
305+
return $xml_string;
306+
}
307+
308+
/**
309+
* Delete a block of text
310+
*
311+
* @param mixed $blockname
312+
* @param string $replacement
313+
*/
314+
public function deleteTemplateBlock($blockname, $replacement = '')
315+
{
316+
$xmlBlock = null;
317+
preg_match('/(<\?xml.*)(<w:p.*>\${'.$blockname.'}<\/w:.*?p>)(.*)(<w:p.*\${\/'.$blockname.'}<\/w:.*?p>)/is', $this->_documentXML, $matchs);
318+
319+
if (isset($matchs[3])) {
320+
$this->_documentXML = str_replace($matchs[2].$matchs[3].$matchs[4], $replacement, $this->_documentXML);
321+
}
322+
}
323+
260324
/**
261325
* Save XML to temporary file
262326
*

0 commit comments

Comments
 (0)