Forum


Replies: 2   Views: 2495
Catch replacevariablebytext internal warning when variable does not exists
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 FGI  · 13-10-2017 - 07:45

Hello,

how can I intercept the warning generated by replaceVariableByText() when I try to replace a variable in the doc which does not exists?

Even if I surround with a try/catch, I still have the warning on stdout, and can't intercept it:

try {

    $docx->replaceVariableByText(['inexistant'=>'...']);

} catch (Exception $e) {

    echo $e->getMessage();

}
Warning: DOMDocument::loadXML(): ParsePI: PI xml0version space expected in Entity, line: 1 in vendor\phpdocx\classes\CreateDocxFromTemplate.inc on line 1943

Call Stack:
    ...
    0.3291   18337616   5. CreateDocxFromTemplate->replaceVariableByText() myclass.class.php:4912
    0.3291   18526032   6. CreateDocxFromTemplate->variable2Text() vendor\phpdocx\classes\CreateDocxFromTemplate.inc:1338
    0.3291   18526032   7. CreateDocxFromTemplate->repairVariables() vendor\phpdocx\classes\CreateDocxFromTemplate.inc:2085
    0.3310   18893696   8. DOMDocument->loadXML() vendor\phpdocx\classes\CreateDocxFromTemplate.inc:1943

Must I replace the logger by a custom behavior which throws exceptions, or something?

(Or is it a bug, as we should never have API internals warnings displayed?)

 

Best regards,

Posted by admin  · 13-10-2017 - 08:14

Hello,

The warning you point out is thrown by PHP; some functions of PHP throw exceptions and others throw notice/warning/fatal messages that can't be catched using a try-catch block (https://stackoverflow.com/questions/7082401/avoid-domdocument-xml-warnings-in-php). If needed, you can register a custom error handler to get that messages and throw exceptions (https://stackoverflow.com/questions/1759069/php-domdocument-error-handling).

Anyway, phpdocx ignores placeholders that don't exist. You could get the placeholders from a template using the getTemplateVariables (https://www.phpdocx.com/api-documentation/templates/get-Word-template-variables) and compare the array this method returns to know if a placeholder exists, and then replace it or not.

But we think there must be an error on the template you are using. If you send to contact[at]phpdocx.com the most simple script and template that illustrates your issue the dev team will check it. The dev team needs to run it, so please avoid external connections, and send the smallest code to be able to test it.

Regards.

Posted by FGI  · 13-10-2017 - 11:40

Thanks. I did a getTemplateVariables(), and displayed a warning if the variable did not exists in the doc.

(By the way: it was "on purpose" that I made a bad template, no worry. I wondered how your code perform if somebody changed the template. For example, if I let users update the template as needed. But thanks for the debugging proposal!)

 

Best regards,