PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

TemplateProcessor.php print empty '' string if value is 0 #2572

Open technilogics opened 4 months ago

technilogics commented 4 months ago

https://github.com/PHPOffice/PHPWord/blob/8b891bb6842dd383f679b47898fad0b7c181f325/src/PhpWord/TemplateProcessor.php#L272

return $subject ? Text::toUTF8($subject) : '';

Cause issue if subject is 0, that actually i want to print, but unfortunately when $subject=0 as string or int or char it results in false.

Please fix this issue

haihuynh98 commented 4 months ago

I also encountered a similar problem, and I haven't been able to solve it yet.

truestealth commented 3 months ago

my way to solve it: override PHPWord/src/PhpWord/TemplateProcessor.php in composer.json and replace return $subject ? Text::toUTF8($subject) : ''; >>> return (null !== $subject) ? Text::toUTF8($subject) : '';

jeremygmajor commented 1 month ago

Thanks @truestealth - works great!

adriansuter commented 3 weeks ago

As a workaround, you could prepend the 0 with a zero width no-break space:

$templateProcessor->setValue('test', mb_chr(hexdec('FEFF')) . '0');