您好,欢迎访问一九零五行业门户网

如何解决php word 乱码问题

php word乱码的解决办法:首先打开“/writer/word2007/base.php”文件;然后添加“$objwriter->writeattribute(‘w:eastasia’, $font)”内容;最后保存修改即可。
推荐:《php视频教程》
phpword解决中文乱码
一、增加东亚字体支持 
打开并编辑路径/writer/word2007/base.php文件内容,大概在第349行(行数随着版本可能会有变化)大概函数_writetextstyle内添加:
$objwriter->writeattribute(‘w:eastasia’, $font)
比如我的修改片段基本是下面这样:
font if($font != ‘arial’) {$objwriter->startelement(‘w:rfonts’);$objwriter->writeattribute(‘w:eastasia’, $font);// 添加这行$objwriter->writeattribute(‘w:ascii’, $font);$objwriter->writeattribute(‘w:hansi’, $font);$objwriter->writeattribute(‘w:cs’, $font);$objwriter->endelement();}
二、 解决中文乱码问题(此解法对于gbk编码有用,不适用于utf-8)
编辑phpword/template.php
找到代码$replace = utf8_encode($replace);,删除或者注释掉这行代码,添加$replace = iconv( ‘gbk’,’utf-8′, $replace);,比如代码改为如下:
if(!is_array($replace)) {//$replace = utf8_encode($replace); $replace =iconv(‘gbk’, ‘utf-8′, $replace);// 注释掉上面行后添加这行}
调用方式如下:
$document->setvalue(‘template’, iconv(‘utf-8′, ‘gb2312//ignore’, ‘中文’));
上面的代码主要解决模板的问题,
下面同样的道理,解决section添加文本的问题,
在phpword/section.php找到代码$giventext = utf8_encode($text);,
删除或者注释掉这行代码,添加$giventext = iconv(‘gbk’, ‘utf-8′, $text);,比如代码如下:
public function addtext($text, $stylefont = null, $styleparagraph = null) {//$giventext = utf8_encode($text); $giventext = iconv(‘gbk’, ‘utf-8′, $text);// 注释掉上面行后添加这行}
以上就是如何解决php word 乱码问题的详细内容。
其它类似信息

推荐信息