Phpword: addText() function and text formatting (new line "\n")

Created on 12 Jun 2015  路  13Comments  路  Source: PHPOffice/PHPWord

Hi,

I have a problem regarding formatting text. Actually, it seems, all \n are not interpreted when opening file with Word 2007. Here are some screenshots :

This is how it appears in LibreOffice :
lo

And how it (sadly) appears in Word 2007 :

w2007

ANy help would be welcome ! I use last stable version - 0.12.0. Thanks !


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Most helpful comment

I used "\n" and then the regex $text = preg_replace('~\R~u', '</w:t><w:br/><w:t>', $text);.

All 13 comments

Hi,

you maybe misunderstood how phpword works.

addText is for adding a paragraph with text. The text is not seperated by line breaks. If you would like to add line breaks then use addTextRun.

With a textrun you can add text and textbreaks within one paragraph.

$text = "foo\nbar\nfoobar";
$textlines = explode("\n", $text);

$textrun = $section->addTextRun();
$textrun->addText(array_shift($textlines));
foreach($textlines as $line) {
    $textrun->addTextBreak();
    // maybe twice if you want to seperate the text
    // $textrun->addTextBreak(2);
    $textrun->addText($line);
}

Thanks for your interest on that.
But why not an optional parameter for commodity ? here is my proposed idea (not sure it works since it is not tested)

How to apply this in the template?
e.g.: $templateProcessor->setValue....

I second morrido. How do you do it in a template?

$text = "foo\nbar\nfoobar";
$textlines = explode("\n", $text);

$textrun = $section->addTextRun();
$textrun->addText(array_shift($textlines));
foreach($textlines as $line) {
$textrun->addTextBreak();
// maybe twice if you want to seperate the text
// $textrun->addTextBreak(2);
$textrun->addText($line);
}

This also works (I just used it) :

$text = "foo\nbar\nfoobar";
$textlines = explode("\n", $text);

for ($i = 0; $i < sizeof($textlines); $i++) {
    $section->addText($textlines[$i]);
}

I, just like @morrido and @cbloss here, would like to know if this is usable in a template. Please answer. Thx in advance

@feakuru @morrido @cbloss
Looks for me like <w:br /> is interpreted as a newline in proper Word.
In libreoffice it just looks like a space (was frustrating me).

so e.g.

$templateProcessor->setValue(name_address, 'John <w:br /> 123 fake street');

I used "\n" and then the regex $text = preg_replace('~\R~u', '</w:t><w:br/><w:t>', $text);.

Awesome @RKaczmarek, exactly what I needed. Thank you. The solution works for addListItemRuns as well as that is what I was using.

@RKaczmarek & @BafS
in my case:
$text='aaa\nbbb'
$text = preg_replace('~\R~u', '', $text);
$templateProcessor->setValue('sp'.$i.'z1#'.$j,$text);

write in word:
'aaa\nbbb'

replace simple quotes by double quotes :

$text="aaa\nbbb"

@feakuru @morrido @cbloss
Looks for me like <w:br /> is interpreted as a newline in proper Word.
In libreoffice it just looks like a space (was frustrating me).

so e.g.

$templateProcessor->setValue(name_address, 'John <w:br /> 123 fake street');

https://github.com/PHPOffice/PHPWord/issues/838

@feakuru @morrido @cbloss
Looks for me like <w:br /> is interpreted as a newline in proper Word.
In libreoffice it just looks like a space (was frustrating me).

so e.g.

$templateProcessor->setValue(name_address, 'John <w:br /> 123 fake street');

As @BafS answered, add \n to the text you want to format, like this:
$templateProcessor->setValue("value_to_change", "value\nthat _will_replace");

After that, go to your TemplateProcessor.php file and find the setValueForPart($ search, $ replace, $ documentPartXML, $ limit). In the first line of this function, add:
$replace = preg_replace('~\R~u', '</w:t><w:br/><w:t>', $replace);

This worked perfectly for me in LibreOffice and Word.
I hope this helps someone.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ryuzakix3 picture Ryuzakix3  路  6Comments

igorsantos07 picture igorsantos07  路  3Comments

btry picture btry  路  4Comments

ahmednawazbutt picture ahmednawazbutt  路  3Comments

Joel-James picture Joel-James  路  3Comments