PHPWord how to add text break / new line while in

2019-04-29 10:12发布

How can I add a text break or go to the next line/row while in a text run? I tried to just do $section->addTextBreak(2); while in the text run but it just added the breaks to the section after the text run. I also tried $textrun->addTextBreak(2); but it gave me a fatal error. Any responses would be greatly appreciated.

8条回答
做自己的国王
2楼-- · 2019-04-29 10:45

For correct work, you need to add a text block:

$string = strtr($string, [
   "\n" => "</w:t>\n<w:br />\n<w:t xml:space=\"preserve\">"
]);
查看更多
相关推荐>>
3楼-- · 2019-04-29 10:47

I'm afraid that this will not be possible with current version. I don't have deep understanding of this library, but from looking at the code, I found out that the textRun class consist only of addText and addLink methods.

But I also need this feature along with several others, so I'm going to write it myself and create a pull request to get it included in the next release (if there will be any).

Basically it can be done by modifying the textRun class, adding an addLineBreak method (similar way as it is in the section class) and then modify class Base.php to create proper elements in final document.

In Docx xml, those line brakes are similar to the html br tag, but previous text must be closed and reopened after using break like this:

<w:r>
  <w:t>This is</w:t>
  <w:br/>
  <w:t xml:space="preserve"> a simple sentence.</w:t>
</w:r>

instead of simply doing

<w:r>
  <w:t>This is<w:br /> a simple sentence</w:t>
</w:r>

So in base.php, you'll need to edit behavior to create this block of code.

Hope this was useful!

EDIT

I have figured out that implementing this is very simple. In textRun.php just add this method:

/**
* Add a TextBreak Element
*
* @param int $count
*/
public function addTextBreak($count = 1) {
    for($i=1; $i<=$count; $i++) {
        $this->_elementCollection[] = new PHPWord_Section_TextBreak();
    }
}

and in Base.php in the _writeTextRun method at the end of this method add this condition:

elseif($element instanceof PHPWord_Section_TextBreak) {
    $objWriter->writeElement('w:br');
}
查看更多
甜甜的少女心
4楼-- · 2019-04-29 10:49

use PHP_EOL for line break it is the only solution for line break in PHPWord

查看更多
闹够了就滚
5楼-- · 2019-04-29 10:54

I don't know if a PR from the accepted answer was incorporated or it was in fact already possible in 2013, but regardless, since 2015 if not earlier, the correct way to insert line breaks within a paragraph is indicated in the initial response to #553 on GitHub:

  • Create a TextRun for the paragraph;
  • Add line breaks to the TextRun by calling the addTextBreak() method of the TextRun itself.

Here's a function that will take care of this for a complete paragraph of text in a string containing literal line-breaks (CR-LF ["\r\n"], LF ["\n"] or CR ["\r"]):

/**
 * @param \PhpOffice\PhpWord\Element\AbstractContainer $container
 *        E.g. a section or table cell.
 * @param string $text String with literal line breaks as CR-LF, LF or CR.
 * @param string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle
 * @param string|array|\PhpOffice\PhpWord\Style\Font $fontStyle
 *
 * @return \PhpOffice\PhpWord\Element\TextRun
 */
function addTextWithLineBreaks(
  \PhpOffice\PhpWord\Element\AbstractContainer $container,
  $text,
  $fontStyle,
  $paragraphStyle
) {
  $textRun = $container->addTextRun($paragraphStyle);
  foreach (preg_split('/(\\r\\n?+|\\n)/',
    $text,
    -1,
    PREG_SPLIT_DELIM_CAPTURE
  ) as $i => $part) {
    if ($i & 1) {
      $textRun->addTextBreak(1, $fontStyle, $paragraphStyle);
    } else {
      $textRun->addText($part, $fontStyle, $paragraphStyle);
    }
  }
  return $textRun;
}

PHPWord (0.14.0) currently discards line breaks when reading Word2007 documents – hopefully that will be fixed before 1.0 – but the output from the above is correct when opened in Word.

查看更多
来,给爷笑一个
6楼-- · 2019-04-29 10:55

You can have a text and add \n where ever you want to have line breaks, and do the rest like this:

$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);
}
查看更多
Juvenile、少年°
7楼-- · 2019-04-29 10:56

It's easy: just start a new textrun, and before it add textbreak to the section, like this (tested with docx format):

$textrun = $section->addTextRun();

$textrun->addText('blah-blah', 'p_bold');

  $section->addTextBreak(2);

  $textrun = $section->addTextRun();

  $textrun->addText('blah-blah-blah in new line ', 'p');

  $textrun->addText('blah-blah', 'p_bold');

  $textrun->addText('blah-blah', 'p');

  $section->addTextBreak(2);

  $textrun = $section->addTextRun();

  $textrun->addText('blahblah', 'p');
查看更多
登录 后发表回答