BackTrack57
BackTrack57

Reputation: 395

PHPWord how to wordwrap text?

I currently have an issue tested in Word2007, Word2013 and it is working fine in LibreOffice.

I have text coming from mysql database inside cells and it doesn't respect the line breaks.

Example in Word2007:
text = - car - plane - sofa - office

Example in LibreOffice (Ubuntu 16 LTS):
- car
- plane
- sofa
- office

I have found an option inside PHPWord 'wordWrap'=>true but doesn't seems to have any effects.

Have someone encounted that issue and could give me a working example please?

EDIT: Trying first solution inside table with cells. (The code below is inside a while loop so it recreate 10 times the cell if there is 10 \n)

$table->addRow($height);
$table->addCell($width, $styleVertical)->addText("$risques_identifies", $styleTextNextRow, $styleLeft);
$table->addCell($width, $styleVertical)->addText("$epi_epc", $styleTextNextRow, $styleLeft);

$table->addCell($width, $styleVertical);
$textlines_organisation_documentation=explode("\n",$organisation_documentation);
for ($i = 0; $i < sizeof($textlines_organisation_documentation); $i++) {
$table->addText("$textlines_organisation_documentation", $styleTextNextRow, $styleLeft);
}

My code create an error. I don't know how to create a cell and add the text after (inside the "for"). This is what i'm trying to do.

Upvotes: 1

Views: 1153

Answers (1)

molivier
molivier

Reputation: 2246

This will insert a line break for each paragraph defined by \n:

Edit (thanks @CBroe):

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

// Create table...
// Add table cell
$mycell = $table->addCell()

// Loop content to append cell
for ($i = 0; $i < sizeof($textlines); $i++) {
    $mycell->addText($textlines[$i]);
}

Upvotes: 2

Related Questions