Search code examples
phpphpword

How to add a textbreak in phpword?


I use the "phpword" class to generate a .doc file, but i have the problem:

how to add a line break for this problem?, so for example I have the following text:

Code (in a variable):

"This is
a text
With line break"

Now if we enter that into the word document...will display this:

"This is a text With line break."

how can i do this? i want the text in the word document with this style:

This is
a text
With line break

Thanks!


Solution

  • This is not possible with phpword. You have to split the string in lines and use addTextBreak.

    <?php
    $text = "This is
    a text
    With line break";
    
    $lines = explode("\n", $text);
    
    foreach ($lines as $line) {
        $doc->addText($line);
        $doc->addTextBreak();
    }