使用PHP/COM将UTF-8字符串写入Word

使用PHP/COM将UTF-8字符串写入Word,php,utf-8,com,ms-word,Php,Utf 8,Com,Ms Word,我正在尝试使用PHP/COM从MySQL数据库生成一个Word文档。如果数据库中的数据是简单的ASCII文本(例如“hello”),它将正确显示在Word文档中。如果数据包含非ASCII(多字节)字符(例如“毛利人”),则它们会正确显示,但末尾有“有趣”字符(例如空、空格或中文符号) 环境:我正在使用Windows7企业版、Apache、MySQL、PHP5.2.17和MicrosoftOffice2010 下面是一个简化的示例-我甚至不使用数据库或写入Word文档,而是简单地使用WordCle

我正在尝试使用PHP/COM从MySQL数据库生成一个Word文档。如果数据库中的数据是简单的ASCII文本(例如“hello”),它将正确显示在Word文档中。如果数据包含非ASCII(多字节)字符(例如“毛利人”),则它们会正确显示,但末尾有“有趣”字符(例如空、空格或中文符号)

环境:我正在使用Windows7企业版、Apache、MySQL、PHP5.2.17和MicrosoftOffice2010

下面是一个简化的示例-我甚至不使用数据库或写入Word文档,而是简单地使用WordCleanString方法重现问题:

private function _cleanString($wordApp, $str)
{
    $vStr = new VARIANT($str, VT_BSTR, CP_UTF8);
    $bytes = strlen($vStr);
    $chars = mb_strlen($vStr, "UTF-8");
    echo "Test string: $vStr (bytes=$bytes, chars=$chars)<br/>";
    $vStr = $wordApp->CleanString($vStr);
    $bytes = strlen($vStr);
    $chars = mb_strlen($vStr, "UTF-8");
    echo "Test string (after cleaning): $vStr (bytes=$bytes, chars=$chars)<br/>";
    echo "<br/>";
}

public function testUtf8Strings()
{
    com_load_typelib('Word.Application');
    // Specifying codepage as CP_UTF8 to let COM/Word know strings I pass in will be in UTF-8 format.
    $wordApp = new COM("word.application", null, CP_UTF8) or die ("couldn't create an instance of word");
    echo "Loaded Word, version {$wordApp->Version} <br/>";
    $wordApp->visible = false;

    echo "<br/>";
    $this->_cleanString($wordApp, 'No multi-byte characters.');
    $this->_cleanString($wordApp, 'Multi-byte chars: Māori 楠 test.');
    $this->_cleanString($wordApp, 'Multi-byte chars: Ā ā Ē ē Ī.');

    $wordApp->Quit(false); // Imortant: must say 'false', otherwise Word does not close
    $wordApp = null;
    echo "Quit Word.";

    return;
}

CleanString方法从给定字符串中删除非打印字符,并将其更改为空格。由于我的字符串已经“干净”,我希望能得到相同的字符串回来。当我的字符串具有多字节字符时,情况并非如此。看起来Word使用原始字符串中的字节数作为返回字符串中的字符数。

原来这是PHP 5.4.29中修复的PHP错误()。我使用PHP5.5.19进行了测试,问题不再出现。HTML输出为:

Loaded Word, version 14.0

Test string: No multi-byte characters. (bytes=25, chars=25)
Test string (after cleaning): No multi-byte characters. (bytes=25, chars=25)

Test string: Multi-byte chars: Māori 楠 test. (bytes=34, chars=31)
Test string (after cleaning): Multi-byte chars: Māori 楠 test. 5⹮ (bytes=39, chars=34)

Test string: Multi-byte chars: Ā ā Ē ē Ī. (bytes=33, chars=28)
Test string (after cleaning): Multi-byte chars: Ā ā Ē ē Ī. 琠獥⹴㔠 (bytes=46, chars=33)

Quit Word.
Loaded Word, version 14.0

Test string: No multi-byte characters. (bytes=25, chars=25)
Test string (after cleaning): No multi-byte characters. (bytes=25, chars=25)

Test string: Multi-byte chars: Māori 楠 test. (bytes=34, chars=31)
Test string (after cleaning): Multi-byte chars: Māori 楠 test. (bytes=34, chars=31)

Test string: Multi-byte chars: Ā ā Ē ē Ī. (bytes=33, chars=28)
Test string (after cleaning): Multi-byte chars: Ā ā Ē ē Ī. (bytes=33, chars=28)

Quit Word. 

我认为这是一个PHP问题,并查看了PHP bug页面,发现了这个bug:显然,它是在PHP5.4.37中修复的。我将升级我的PHP版本并试一试。关于上面的评论,这个错误在PHP5.4.29中得到了修复。