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 :

And how it (sadly) appears in Word 2007 :

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.
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', '
$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');
@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.
Most helpful comment
I used
"\n"and then the regex$text = preg_replace('~\R~u', '</w:t><w:br/><w:t>', $text);.