Phpword: Error with addHtml() and "&"

Created on 5 Nov 2014  路  8Comments  路  Source: PHPOffice/PHPWord

Hi,

I'm trying to create a doc:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $content);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('...');

where $content contains a string with a "&".

I get this error:

Warning: DOMDocument::loadXML(): xmlParseEntityRef: no name in Entity, line: 1 in PhpOffice/PhpWord/Shared/Html.php on line 55
Notice: Trying to get property of non-object in PhpOffice/PhpWord/Shared/Html.php on line 129
Notice: Trying to get property of non-object in PhpOffice/PhpWord/Shared/Html.php on line 169
Notice: Trying to get property of non-object in PhpOffice/PhpWord/Shared/Html.php on line 170

I've tried to replace the & by & but same error. If I remove it, no problem.

I'm using the last version of PHPWord.

Thank you

Bug Report HTML Open XML (Word 2007+)

Most helpful comment

For anyone else encountering this issue, In later versions (0.13 onwards?) you can turn on output escaping which fixes the issue:

Settings::setOutputEscapingEnabled(true)

All 8 comments

Do you use the latest code of PHPWord?
Compare the file Html.php with
https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/Shared/Html.php
at lines 39 - 59.

Give the example content which causes the error message.

With $content = '&' ou $content = '&', there is an error.

I'm using the version available on master and the Html.php is different for the lines 39 - 59.

Use the file Html.php from the developer branch. This issue should be fixed then.

I changed it and it works fine, thanks !

If I understand right, the problem was fixed here.

Hi
I am new to PHPWord and trying to create a doc. I am running PHP 5.6.0 and using latest PHPWord but the document is not created when there is an & and or &amp. I am getting illegal name character error.
This is part of my code:
//html text
$results = $key['data'][COMPPERSCOPY];
\PhpOffice\PhpWord\Shared\Html::addHtml($section,$results);

It is working fine when i remove & and &amp. Please help me.
Thank you.

For anyone else encountering this issue, In later versions (0.13 onwards?) you can turn on output escaping which fixes the issue:

Settings::setOutputEscapingEnabled(true)

That's: \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);

Was this page helpful?
0 / 5 - 0 ratings