Skip to content

TemplateProcessor dollar sign issue #1022

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

Closed
krtorio opened this issue Mar 16, 2017 · 2 comments
Closed

TemplateProcessor dollar sign issue #1022

krtorio opened this issue Mar 16, 2017 · 2 comments

Comments

@krtorio
Copy link

krtorio commented Mar 16, 2017

I have a docx template file going through the Template Processor, but the resulting output docx file has an error and cannot be opened.

Further inspection revealed that the error is caused by individual dollar sign or $ characters located anywhere in the docx file. Output docx file is now readable after removing $ characters from the template.

Is this something to do with the ${search pattern}? Does phpword expect $ characters to be followed by open close brackets?

The docx template file is a financial document so $ characters are expected to show up everywhere. I am surprised that the issue exists I expected phpword to ignore $ characters that do not follow the pattern.

@ozilion
Copy link
Contributor

ozilion commented Mar 16, 2017

@krtorio you can use any other preffix and suffix instead of ${ and } just modifying two function in TemplateProcessor as below but i must say i am using version 12 so you shouldn't just copy paste the code
`
public function setValue($search, $replace, $preffix = "${", $suffix = "}", $limit = -1)
{
foreach ($this->temporaryDocumentHeaders as $index => $headerXML) {
$this->temporaryDocumentHeaders[$index] = $this->setValueForPart($this->temporaryDocumentHeaders[$index], $search, $replace, $preffix, $suffix, $limit);
}

    $this->temporaryDocumentMainPart = $this->setValueForPart($this->temporaryDocumentMainPart, $search, $replace, $preffix, $suffix, $limit);

    foreach ($this->temporaryDocumentFooters as $index => $headerXML) {
        $this->temporaryDocumentFooters[$index] = $this->setValueForPart($this->temporaryDocumentFooters[$index], $search, $replace, $preffix, $suffix, $limit);
    }
}

`
and

`protected function setValueForPart($documentPartXML, $search, $replace, $preffix = '${', $suffix = '}', $limit)
{
$pattern = '|' . $preffix . '([^' . $suffix . ']+)' . $suffix . '|U';
//$pattern = '|${([^\}]+)}|U';
preg_match_all($pattern, $documentPartXML, $matches);
foreach ($matches[0] as $value) {
$valueCleaned = preg_replace('/<[^>]+>/', '', $value);
$valueCleaned = preg_replace('/</[^>]+>/', '', $valueCleaned);
$documentPartXML = str_replace($value, $valueCleaned, $documentPartXML);
}
$preffix = str_replace("\", "", $preffix);
$suffix = str_replace("\", "", $suffix);

    /*        if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
                $search = '${' . $search . '}';
            }*/
    if (substr($search, 0, 2) !== $preffix && substr($search, -1) !== $suffix) {
        $search = $preffix . $search . $suffix;
    }

    if (!SharedString::isUTF8($replace)) {
        $replace = utf8_encode($replace);
    }

    $regExpDelim = '/';
    $escapedSearch = preg_quote($search, $regExpDelim);
    return preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u", $replace, $documentPartXML, (int)$limit);
}`

@krtorio
Copy link
Author

krtorio commented Mar 17, 2017

I'm using version 0.13.0, thanks for the warning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants