Phpword: cloneBlock bug with subsequent document tables

Created on 12 Apr 2019  路  7Comments  路  Source: PHPOffice/PHPWord

This is:

  • [x ] a bug report
  • [ ] a feature request
  • [ ] not a usage question (ask them on https://stackoverflow.com/questions/tagged/phpword)

Expected Behavior

I have a table which I have enclosed with cloneblock markers. This table is effectively in the middle of my document. The expectation is that cloneblock will clone it however many times I specify irrespective of what other data proceeds it in the document.

Current Behavior

If another arbitrary table exists in the document after the cloneblock marks, the cloneblock operation does not work

Failure Information

There is no failure

How to Reproduce

Please provide a code sample that reproduces the issue.

  1. Create a word document with a table inbetween cloneblock flags. When the cloneBlock function runs it works.
  2. Create a word document with a inbetween cloneblock flags. Additionally create a page break in the document after the last cloneblock marker. Create another table in this document. When executed the cloneblock function will not work.

Context

  • PHP version:
  • PHPWord version: 0.14 (I have updated to the latest cloneblock function)

Most helpful comment

My issue was the block i was trying to clone was too large for preg_match. I was able to fix this by adjusting the following setting ini_set("pcre.backtrack_limit", "10000000");

All 7 comments

On slightly further investigation, I'm not sure what's going on. I created a very basic template and I couldn't reproduce my bug, it worked as I would expect it to work. However, I have encountered the issue on two other different templates. The regular expression in the preg_match of the cloneBlock function doesn't find the $blockname when a subsequent table is added to those documents. I don't get it.

Edit: Narrowing things down further ... if the subsequent table after the cloneblock markers has 1 or 2 columns, the clone operation will work. If the subsequent table after the cloneblock markers has 3+ columns, the cloneblock operation will not work.

I hope I have explained all of that ok.

I have same problem too.
When table rows > 3,the cloneblock function will not work.

I try to fix it,@shakenbakesl you can try this code,edit TemplateProcessor.php PHPWord version: 0.16

public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false, $variableReplacements = null)
    {
        $xmlBlock = null;
        $matches = array();
        preg_match(
            '/(<w:p\b.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p\b.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
            $this->tempDocumentMainPart,
            $matches
        );
        if (isset($matches[2])) {
            $xmlBlock = $matches[2];
            if ($indexVariables) {
                $cloned = $this->indexClonedVariables($clones, $xmlBlock);
            } elseif ($variableReplacements !== null && is_array($variableReplacements)) {
                $cloned = $this->replaceClonedVariables($variableReplacements, $xmlBlock);
            } else {
                $cloned = array();
                for ($i = 1; $i <= $clones; $i++) {
                    $cloned[] = $xmlBlock;
                }
            }

            if ($replace) {
                $this->tempDocumentMainPart = str_replace(
                    $matches[1] . $matches[2] . $matches[3],
                    implode('', $cloned),
                    $this->tempDocumentMainPart
                );
            }
        }

        return $xmlBlock;
    }

Thanks Mrrennen, I'll take a look at your code. For some reason I can't understand, the problem seemed to fix itself. I suffered a hard disk failure in the last few weeks and had to migrate my code to a new system. After that, it just started to work again!? :/

I am experiencing the same thing. I can get my second table to 3 columns by 2 rows. Oddly I have no issues having it be 6x11 on my local wamp stack, but it will only do 3x2 on my linux test server.

My issue was the block i was trying to clone was too large for preg_match. I was able to fix this by adjusting the following setting ini_set("pcre.backtrack_limit", "10000000");

ini_set("pcre.backtrack_limit", "10000000");

@rybo333 save the day.

v: 0.16.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

totpero picture totpero  路  6Comments

dwalker109 picture dwalker109  路  6Comments

Joel-James picture Joel-James  路  3Comments

cedrictailly picture cedrictailly  路  5Comments

phuwin picture phuwin  路  3Comments