1

Using PhpWord in my PHP project gives me issues. I do have a variable containing CR/LF (\n) and HTML code. Something like:

$str='<div style="font-style: italic; text-align:center">Line1
line2
line3</div>';

The text is comming out of a MySql text field containing \n as line separator.

When processing this text with PhpOffice\PhpWord\Shared\Html::addHtml, I am losing the CR/LF. All text is concatenated. When replacing \n with html CRLF $str=str_replace("\n","<br>",$str); and then use addHtml, it gives me run-time errors:

Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: br line 1 and div in Entity, line: 1 in /customers/7/4/3/ofiser.be/httpd.www/vieringen/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php on line 94

How can I process this text? Any help is appreciated.

7
  • 6
    loadXML expects well-formed XML, so you can't have unclosed tags - use <br /> Commented Oct 9 at 9:27
  • 1
    Why not use nl2br() PHP function? This would need to go at point of display after any XML handling/encoding. Commented Oct 9 at 11:10
  • Works fine, but addHtml inserts 2 linebreaks for each <\br>? Commented Oct 9 at 11:19
  • I agree, should have used NL2BR but now addHtml generates 2 hard rerturns for each line of text? Commented Oct 9 at 11:45
  • 1
    There is no </br> closing not in XML or xHTML. It is still an empty tag. The tag needs a trailing slash as C3roe said: <br /> Commented Oct 9 at 20:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.