Forum


Replies: 7   Views: 3542
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

 

Posted by lotveito  · 28-11-2016 - 10:57

Shortly after posting I got an idea: I normally write in Norwegian language, and even if the templates just have a few words exept for the variables, these words are in Norwegian. I have now looked at my 4 different templates and they all contain the Norwegian character written as an "a" with a circle above: å 

I tried making a template without this character, and then everyting worked fine. So my guess is this is an encoding issue. I will look into this further and post my results here.

Posted by admin  · 28-11-2016 - 11:07

Hello,

Other important point about replacing placeholders is that in the same paragraph you can't have a placeholder with the same name that an existing string.

For example:

$ALFA$ This is the alfa paragraph.

Please use other placeholder value such as:

$VAR_ALFA$ This is the alfa paragraph.

Regards.

Posted by lotveito  · 28-11-2016 - 11:18

My findings:

  • My php file is UTF8 encoded. And it has the three things in the start to signal that it is UTF8. I tried to remove these but it made no difference. I then made the phpscript again from scratch in another text-editor. No difference. (First text editor: NetBeans, Second text editor: UltraEdit)
  • I can easily put norwegian characters (å, ø, æ) in my php code, and replace the variables in the templates with norwegian text that includes these characters. This works beautifully.
  • But whenever there are one or more norwegian characters somewhere in the template word-file the first replacement fails.
  • The template is made in Word 2016. From Word I tried to save the template as "Strict open XML-document (*.docx)" but it made no difference.
  • Originally I did not have tidy intalled. I have installed tidy, and it made no difference.

Here is the info from check.php:

PHP_VERSION: 5.3.3-7+squeeze19
PHP_OS: Linux
PHP_UNAME: Linux bhtserver 2.6.32-5-686 #1 SMP Tue May 13 16:33:32 UTC 2014 i686
SERVER_NAME: mgyq.time.bedriftshelsen.no
SERVER_SOFTWARE: Apache/2.2.16 (Debian)
SERVER_ADDR: 192.168.15.51
SERVER_PROTOCOL: HTTP/1.1
HTTP_HOST: mgyq.time.bedriftshelsen.no
HTTP_X_FORWARDED_FOR: 
PHP_SELF: /test/phpdocx/check.php
ZipArchive: 1
DomDocument: 1
SimpleXMLElement: 1
Tidy: 1

---------------
I am now about to give up...

Posted by lotveito  · 28-11-2016 - 11:23

Thank you. I did have it in the previos paragraps. My template was something like this:

Alfa will come here:

$ALFA$

Bravo will come here:

$BRAVO$

and so on..

I have now removed all references to the words alfa, bravo and charlie from the template, and my problem still remains.

Posted by admin  · 28-11-2016 - 11:47

Hello,

Which version and license of phpdocx are you using?

Phpdocx is fully tested with RTL and LTR languages: english, spanish, german, swedish, chinese, arabic...

Regards.

Posted by lotveito  · 28-11-2016 - 13:19

Thank you for your efforts to help me on this issue.

I downloaded some time ago, but never had time to start testing until now. The version I have been using is version 6.0. I have now downloaded the newest trial version (v 6.5) and installed it. Sadly this did not solve my problem.

I am still testing, so I do not have a licence. If you believe this problem can be solved, then I would happily purchase now, and continue testing in the full version. But if it can not work with templates in my language, then it is useless to me and I would obviously rather not purchase.

It is good that it have been tested in Sweedish. The character that triggered my problem "å", is a character shared between Sweedish and Norwegian (These languages are very similar).
Norwegian have 3 special characters: å, æ, ø.
Swedish also have 3 special characters: å, ä, ö

Do you think it is doable in Norwegian so I can go ahead with purchasing, or do you recomend testing until it works before purchase?

Posted by admin  · 28-11-2016 - 13:30

Hello,

Yes, we're absolutely sure it should work fine.

If you send to contact[at]phpdocx.com a DOCX template and a PHP script we can check it directly. Please send the smallest script that illustrate your issue without doing external connections; this is just DOCX template and the PHP that replace the placeholders.

Regards.