Forum


Replies: 4   Views: 1040
Bulkprocessing replace all variables in the same text tag
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 18-11-2021 - 10:58

Hello,

Thanks for your feedback. The current code of the testing branch of that method in BulkProcessing is the following:

protected function variable2Text($variables, $dom, $options)
{
    if (PHP_VERSION_ID < 80000) {
        $optionEntityLoader = libxml_disable_entity_loader(true);
    }
    $dom = simplexml_load_string($dom->saveXML());
    if (PHP_VERSION_ID < 80000) {
        libxml_disable_entity_loader($optionEntityLoader);
    }
    $dom->registerXPathNamespace('w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');

    if (isset($options['firstMatch'])) {
        $firstMatch = $options['firstMatch'];
    } else {
        $firstMatch = false;
    }
    foreach ($variables as $variable => $value) {
        $search = $this->templateSymbolStart . $variable . $this->templateSymbolEnd;
        $query = '//w:t[text()[contains(., "' . $search . '")]]';
        if ($firstMatch) {
            $query = '(' . $query . ')[1]';
        }
        $foundNodes = $dom->xpath($query);
        foreach ($foundNodes as $node) {
            $strNode = (string) $node;
            if ($options['parseLineBreaks']) {
                //  replace line breaks
                $value = str_replace(array('\n\r', '\r\n', '\n', '\r', "\n\r", "\r\n", "\n", "\r"), '__LINEBREAK__', $value);
            }

            $strNode = str_replace($search, $value, $strNode);
            $node[0] = $strNode;
        }
    }

    return $dom;
}

to avoid reloading the DOM content for each variable.

Regards.