问题描述
我正在尝试使用 PHPWord 创建一个 Word 文档,其中包含从MySQL 数据库.数据库有 MySQL 字符集:UTF-8 Unicode
(utf8)MySQL 连接整理:utf8_unicode_ci
表字段也是如此.
I'm trying to use PHPWord to create a word document that will include dynamic data pulled out from a MySQL database. The database has MySQL charset: UTF-8 Unicode
(utf8)MySQL connection collation: utf8_unicode_ci
and so does the table fields.
数据在 HTML 中存储和预览良好,但是当使用阿拉伯语变量创建文档时,Word 中的输出看起来像 Ø£ØÙد Ùبار٠اÙÙرÙ
.
Data is stored and previewed fine in HTML, however when creating the document with the arabic variables, the output in Word looks like Ø£ØÙد Ùبار٠اÙÙرÙ
.
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('templates/.../wtvr.docx');
$document->setValue('name', $name);
$document->setValue('overall_percent_100', $overall_percent_100);
$document->save('Individual Report - ' . $name . '.docx');
有没有办法解决这个问题?
Is there anyway to fix that?
推荐答案
嗯,是的.但不幸的是,您必须修改库.该库的作者使用 utf8_encode/utf8_decode
显然完全不了解它们的作用.
Well, yes. But you must unfortunately modify the library. The author of the library uses utf8_encode/utf8_decode
obviously without understanding what they do at all.
在 Shared/String.php
的第 150 行:
On line 150, of Shared/String.php
:
替换
public static function IsUTF8($value = '') {
return utf8_encode(utf8_decode($value)) === $value;
}
与
public static function IsUTF8($value = '') {
return mb_check_encoding($value, "UTF-8");
}
那么,如果你这样做了
$ grep -rn "utf8_encode" .
在项目根目录中,您将找到使用 utf8_encode
的所有行.你会看到像
On the project root, you will find all lines where utf8_encode
is used. You will see lines like
$linkSrc = utf8_encode($linkSrc); //$linkSrc = $linkSrc;
$givenText = utf8_encode($text); //$givenText = $text;
您可以简单地删除注释中显示的 utf8_encode
.
You can simply remove the utf8_encode
as shown in the comments.
为什么utf8_encode/utf8_decode
是错误的?首先,因为那不是他们所做的.他们做from_iso88591_to_utf8
和from_utf8_to_iso88591
.其次,ISO-8859-1 几乎从未使用过,通常当有人声称他们使用它时,他们实际上使用的是 Windows-1252.ISO-8859-1 是一个非常小的字符集,甚至无法编码€
,更不用说阿拉伯字母了.
Why is utf8_encode/utf8_decode
wrong? First of all, because that's not what they do. They do from_iso88591_to_utf8
and from_utf8_to_iso88591
. Secondly, ISO-8859-1 is almost never used, and usually when someone claims they use it, they are actually using Windows-1252. ISO-8859-1 is a very tiny character set, not even capable of encoding €
, let alone arabic letters.
您可以通过以下方式快速查看图书馆:
You can do fast reviews of a library by doing:
$ grep -rn "utf8_\(en\|de\)code" .
如果你得到匹配,你应该继续寻找其他图书馆.这些函数只是每次都做错事,即使有人需要一些边缘情况来使用这些函数,当你真的需要 ISO-8859-1 时最好明确说明它,因为你通常不会这样做.
If you get matches, you should move on and look for some other library. These functions simply do the wrong thing every time, and even if someone needed some edge case to use these functions, it's far better to be explicit about it when you really need ISO-8859-1, because you normally never do.
这篇关于PHPWord:创建一个从右到左的阿拉伯语 word 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!