Forum


Replies: 7   Views: 3536
Problems replacing the first variable
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 lotveito  · 28-11-2016 - 10:50

I have a docx file to use as a templet. It has 3 placeholders $ALFA$, $BRAVO$ and $CHARLIE$ mixed in with some text.

My script looks like this:

$pathtolib="../../lib/";
require_once($pathtolib."phpdocx/classes/CreateDocx.inc");
$docx = new CreateDocxFromTemplate($pathtolib.'phpdocx/templates/testmal3_2.docx');
$docx->replaceVariableByText(array('ALFA' => 'John Smith'));
$docx->replaceVariableByText(array('BRAVO' => 'Something here'));
$docx->replaceVariableByText(array('CHARLIE' => 'Be happy'));
$docx->createDocx('output/t3');

In the outputed docx-file BRAVO and CHARLIE have been substituted fine, but ALFA have not been replaced. In stead where it said $ALFA$ in the template, it now reads: ALFAA$

If I change the order in my code to replace BRAVO before ALFA, the code looks like this:

require_once("../../lib/phpdocx/classes/CreateDocx.inc");
$docx = new CreateDocxFromTemplate('../../lib/phpdocx/templates/kortmal.docx');
$docx->replaceVariableByText(array('BRAVO' => 'Something here'));
$docx->replaceVariableByText(array('ALFA' => 'John Smith'));
$docx->replaceVariableByText(array('CHARLIE' => 'Be happy'));
$docx->createDocx('output/t5');
echo "phpdocxtest<br>";

Now $ALFA$ and $CHARLIE$ are replaced perfectly, but $BRAVO$ is not replaced, but changed to: BRAVOO$

I have tried many variants, and I have made several templates with different content, some extremely simple with just a few words in addition to the variables. No matter what I do it seems the first replace always fails, while all the others work fine. I tried one with 10 replacements, and pictures, header, footer, tables and pictures in the templates. All replacements but the first one worked fine.

Any suggestions to how I can make the first replacement work would be greatly appreciated.

Yours,
Lars Olav Tveito