Home > Article > Backend Development > phpword replaces template content and solves Chinese garbled characters
The content of this article is to share with you the content of phpword replacement template and solve the problem of Chinese garbled characters. Friends in need can take a look.
Okay, download it first:
Download address: https://pan.baidu.com/s/1upup2rpLTVXIdJpGi9kK6A
Password: qxsd
Manual http://phpword.readthedocs.io /en/latest/search.html?q=setValue&check_keywords=yes&area=default
//Use Vendor to include the PHPWord class
require_once "./word/PHPWord.php";
//Initialize PHPWord Object
$PHPWord = new PHPWord();
$oMember['m_real_name'] = "Electron Microscope Test";
$oMember['content'] = "Electron Microscope Test Requirements Description Electron Microscope Test Requirements Description Electron Microscope Test Requirement DescriptionElectronic Microscope Test
Requirement DescriptionElectronic Microscope TestRequirementDescriptionElectronic Microscope TestRequirementDescriptionElectronic Microscope TestRequirementDescriptionElectronic Microscope TestRequirementDescriptionElectronic Microscope TestRequirementDescription
Electronic Microscope TestRequirementDescriptionElectronic Microscope TestRequirementDescriptionElectronic Microscope TestRequirementDescriptionElectronic Microscope TestRequirement Describe electron microscopy test requirementsDescribe electron microscopy test requirementsDescribe electron microscopy test requirements
Describe electron microscopy test requirementsDescribe electron microscopy test requirementsDescribe electron microscopy test requirementsDescription";
//Load template file
$document = $PHPWord ->loadTemplate('./word/test/template1.docx');
//Replace a content in the Word template file. If you want to display Chinese here, you must use the iconv function
$document->setValue('need_name', iconv('utf-8', 'GB2312//IGNORE', $oMember['m_real_name']));
$document->setValue('content', iconv('utf-8', 'GB2312//IGNORE', $oMember['content']));
//Replace a content in the Word template file, do not use the Chinese setValue
$document->setValue('file', "7897898998");
$filename = './word/test/m-i-'.time().'.docx';
//Save Word document
$document->save($filename);
//Output download Word document
//header("Location: ".$filename)
How to write variables in template files
content =》 ${content} Remember not to add any style to this variable otherwise it will be affected
need_name =》 ${need_name}
file =》 ${file}
You will find that the Chinese characters are garbled. You need to modify several files:
Word/Base.php _WriteTextStyle() method below
$objWriter->writeAttribute('w:eastAsia', $font); //Modify and add
if ($font != 'Arial') {
$objWriter-> Modify and add this line
$objWriter->writeAttribute('w:ascii', $font);
$objWriter->writeAttribute('w:hAnsi', $font);
$objWriter ->writeAttribute('w:cs', $font);
$objWriter->endElement();
}
PHPWord/Template.php setValue() method :
if(!is_array($replace)) {// $replace = utf8_encode($replace); Note
$replace = iconv( 'gbk','utf- 8', $replace); //New
}
How does php solve the problem of Chinese garbled characters?
The above is the detailed content of phpword replaces template content and solves Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!