Template setValue with caractère "&" - File corrupt

Aug 1, 2014 at 9:50 AM
I'm developping a web page in which the users can download a word file.
I'm trying to do this with PHPWord. But the file .docx that outputs from the page is corrupt and I can't open it. The problème is the caractere "&".
Here's the script:

require_once 'PHPWord.php';
$PHPWord = new PHPWord();

$template = $PHPWord->loadTemplate('Client.docx');
$client = "Ben&Nuts";
$template->setValue('CLIENTE', $cliente);

Why and how resolve it ?

Thank for the response.
Aug 28, 2014 at 3:32 PM
I find a tempory solution!

use the function htmlentities

$template = $PHPWord->loadTemplate('Client.docx');
$client = htmlentities("Ben&Nuts");
$template->setValue('CLIENTE', $cliente);
Aug 28, 2014 at 4:07 PM
I'm sorry for my first reply.

htmlentities resolve the problem with "&" but when you have "é" or other accented letter, the output from the page is corrupt.

I find only the problem with "&" and not with an other special caractere...

I propose this solution :
$client = str_replace("&", "&","Ben&Nuts");
Aug 29, 2014 at 8:24 AM
Other caractere....
$client = str_replace(array("&",'>','<'), array("&amp;","&gt;","&lt;"), $client);
Aug 29, 2014 at 10:04 AM