Skip to content
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

Open
robtrooper opened this issue Nov 18, 2020 · 5 comments · May be fixed by #2221
Open

deleteBlock fails and creates a corrupt doc #1970

robtrooper opened this issue Nov 18, 2020 · 5 comments · May be fixed by #2221

Comments

@robtrooper
Copy link

robtrooper commented Nov 18, 2020

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

  • PHP Version: 7.2
  • PHPWord Version: 0.18

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
);

@manukieli
Copy link

I've the same issue with php 7.3

@ikkysleepy
Copy link

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.

@cnpetrich
Copy link

Same problem with PHP 7.4. Thanks @robtrooper for the fix! I seem to remember having to do something similar in 0.17.0.

@porquero
Copy link

porquero commented Sep 9, 2021

It works! Thanks!

@bc-ts
Copy link

bc-ts commented Jan 28, 2022

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

tontonsb added a commit to tontonsb/PHPWord that referenced this issue May 9, 2022
@tontonsb tontonsb linked a pull request May 9, 2022 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

6 participants