如何解决php word 乱码问题-PHP问题

资源魔 31 0

php word乱码的处理方法:起首关上“/Writer/Word2007/Base.php”文件;而后增加“$objWriter->writeAttribute(‘w:eastAsia’, $font)”内容;最初保留修正便可。

保举:《PHP视频教程》

PHPword处理中文乱码

1、添加东亚字体支持

关上并编纂门路/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();
}

2、 处理中文乱码成绩(此解法关于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 乱码成绩的具体内容,更多请存眷资源魔其它相干文章!

标签: php 乱码 php教程 php故障解决 php使用问题 word

抱歉,评论功能暂时关闭!