Right-to-Left languages

Jan 25, 2014 at 8:57 AM

I've a mysql database localization table with text fragments in various languages.
They're already in UTF-8.

I'm trying to put these text fragments in a word table.

All languages are displayed fine with PHPWord, except those Right-to-Left (hebrew, arabic, ...) which are displayed left-to-right. There's no garbage; they're just not right-to-left in the docx generated by PHPWord.

I've tried either with 0.6.2-1 and with the 0.7.0 from github.

Any idea how could I handle RTL languages?

Is it possible to put a raw "manually" encoded piece of xml in a textrun or table cell?

Thank you

Jan 25, 2014 at 10:28 AM
Ok, it seems like I kinda did it by myself.

Maybe it's primitive and needs further work (for example, stuff like <w:lang w:bidi=”xx-YY”/> where xx-YY is the locale) but it works.

The solution is adding <w:rtl/> XML element before the Right-To-Left text.

Go to class PHPWord_Style_Font in file PHPWord/Style/Font.php:
// add this field after private $_fgColor;
private $_rtl;

// add this line in the constructor "public function __construct"
// after $this->_fgColor = null;
$this->_rtl = null;

// add these two methods at the end of the class

public function getRtl() {
    return $this->_rtl;

public function setRtl($pValue = false) {
    if($pValue == '') {
        $pValue = false;
    $this->_rtl = $pValue;
    return $this;
Go to class PHPWord_Writer_Word2007_Base in file PHPWord/Writer/Word2007/Base.php:
// in method protected function _writeTextStyle
// add after if ($font != 'Arial') { ... } 

// RTL
if(!is_null($rtl)) {
    // <w:rtl/>
To use it:
$section->addText('مرحبا العالم', array('rtl' => true)); // writes "hello world" in arabic according to google translator :-)