-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deleteBlock fails and creates a corrupt doc #1970
Comments
I've the same issue with php 7.3 |
Good Catch. I have PHP 7.4 and for large Blocks of Word document the delete does not work. I used the clone block and it's working now for large block of word documents with multiple tags. |
Same problem with PHP 7.4. Thanks @robtrooper for the fix! I seem to remember having to do something similar in 0.17.0. |
It works! Thanks! |
A turnaround that's working for me is using cloneBlock method with kinda deleting parameters like so : $template->cloneBlock('BLOCK_NAME', 0, true, false, null); With clone count = 0, replace = true and data = null it completely deletes the block and don't corrupt the file because as @robtrooper mentionned, the regex used in cloneBlock is capturing all and is not corrupting file as the one in deleteBlock. I would like to say thank you for noticing this on github, it saved me a lot of time and i hope it would be patched soon |
Describe the Bug
Using $temProc->deleteBlock('test_hide_blk'); to delete a block but when trying to open file Word reports that file is corrupt.
Looking at the document.xml the parser is missing part of the xml in the removal.
Steps to Reproduce
$temProc = new \PhpOffice\PhpWord\TemplateProcessor("/path/to/template.docx");
$temProc->deleteBlock('test_hide_blk');
template.docx =
${test_hide_blk}
Some text to remove.
${/test_hide_blk}
Expected Behavior
That my test_hide_blk block is removed in a usable document.
Current Behavior
Block is only partially removed with a corrupt doc.
Context
I can fix this by copying the preg_match from cloneBlock into replaceBlock.
public function replaceBlock($blockname, $replacement)
{
$matches = array();
// preg_match( // previous preg_match
// '/(<?xml.)(<w:p.>${' . $blockname . '}</w:.?p>)(.)(<w:p.${/' . $blockname . '}</w:.?p>)/is',
// $this->tempDocumentMainPart,
// $matches
// );
preg_match( // from cloneBlock
'/(<?xml.)(<w:p\b.>${' . $blockname . '}</w:.?p>)(.)(<w:p\b.${/' . $blockname . '}</w:.?p>)/is',
$this->tempDocumentMainPart,
$matches
);
The text was updated successfully, but these errors were encountered: