PHPWord:创建从右到左的阿拉伯语单词文档 [英] PHPWord: Creating an Arabic right to left word document

查看:129
本文介绍了PHPWord:创建从右到左的阿拉伯语单词文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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');

反正有解决办法吗?

推荐答案

是的.但是很遗憾,您必须修改该库.该库的作者显然使用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.

第150行,位于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_utf8from_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:创建从右到左的阿拉伯语单词文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆