Forum


Replies: 1   Views: 462
Ampersand bug
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpxlsx 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 phpxlsx version and check the Documentation available.

Posted by KaiVoigt  · 14-10-2022 - 07:03

There is an issue with ampersands. When creating a simple xlsx sheet with just one $val$ cell, the following code creates a defective excel file.

$xlsxStructure = new XlsxStructure();
$xlsxStructure->parseXlsx($datasource);

$xlsx = new CreateXlsxFromTemplate($xlsxStructure);
$xlsx->replaceVariableText(array('val' => 'Hi & Welcome'));

$xlsx->saveXlsx($newXlsxFile);

The problem here is the ampersand.

Instead I need to use the following.

$xlsx->replaceVariableText(array('val1' => 'Hi & Welcome'));

There should be an appropriate replacement internally.

Posted by admin  · 14-10-2022 - 08:16

Hello,

All methods that add contents apply the htmlspecialchars automatically to convert special characters. The replaceVariableText doesn't apply htmlspecialchars automatically to the new values. A patch to apply htmlspecialchars in replaceVariableText values has been added into the testing branch to be included in the stable release.

Using the current stable release, please apply it manually:

$xlsx->replaceVariableText(array('val1' => htmlspecialchars('Hi & Welcome')));

Regards.