Search code examples
phpphpword

one line with multiple color text in phpword?


Is there any method to write different color of text in a same line like this but it change whole line color?

I want my line to like this:
enter image description here

Here is my code:

$section->addText( 'Headline: ', (array('color'=>'#70AD47')),$fontStyleIndexPara);
            $section->addText(cleanstring($data[$k]['ArticleTitle']),$fontStyleIndexPara);
           `

Solution

  • Instead of writing the individual text parts directly into the section with addText(), enclose it with addTextRun().

    By using addTextRun() you can prevent the line break.

    Code for your example:

    $TextRun = $section->addTextRun();
    $TextRun->addText( 'Headline: ', array('color'=>'70AD47'));
    $TextRun->addText(cleanstring($data[$k]['ArticleTitle']));
    

    The # is not used for the color.