Non-general Phpword Chinese garbled problem

Source: Internet
Author: User
Tags phpword
In PHP with the Phpword library to generate Word documents, there will be garbled, I found a lot of information on the Internet, according to the above steps, modified the template.php in the
Public Function SetValue ($search, $replace) {
if (substr ($search, 0, 2)!== ' ${' && substr ($search,-1)!== '} ') {
$search = ' ${'. $search. '} ';
}

if (!is_array ($replace)) {
$replace = Utf8_encode ($replace);
$replace =iconv (' GBK ', ' utf-8 ', $replace);
}
Replace the Utf8_encode inside with Iconv (' GBK ', ' utf-8 ', $replace);

Also to the text transcoding, in the data read from the database also transcoding, written in the Word document or transcoding, but in the generation of word when the Chinese became garbled, transcoding is from GBK to Utf-8.
Please the hero to help my brother two strokes, thank you ah.


Reply to discussion (solution)

Big Brother, help, I tried a lot of methods is not ....

Database encoding format? Page encoding format? What's it all about?

Database encoding format? Page encoding format? What's it all about?
It's all utf-8.

Reference 3 Floor Ohmygirl's reply:
Database encoding format? Page encoding format? What's it all about?

It's all utf-8.


Word's default encoding format should be GBK, you try to write Word documents with GBK encoding (Word document content with GBK, page can continue to use your utf-8)

Word's default encoding format should be GBK, you try to write Word documents with GBK encoding (Word document content with GBK, page can continue to use your utf-8)
I've tried this, and after turning Utf-8 into GBK, it's empty when I write to word, but it has data.

Then you write the Word document that piece of code to look at ~ ~

Then you write the Word document that piece of code to look at ~ ~
if (In_array (' 7 ', $a, TRUE)) {
$table->addcell ($styleCell)->addtext (' station code ', $fontStyle);
$table->addcell ($styleCell)->addtext (iconv (' utf-8 ', ' GBK ', ' Station name '), $fontStyle);
$table->addcell ($styleCell)->addtext (' river system ', $fontStyle);
$table->addcell (2500, $styleCell)->addtext (' To quote time ', $fontStyle);
$table->addcell ($styleCell)->addtext (' Water level ', $fontStyle);
$table->addcell ($styleCell)->addtext (' Water potential ', $fontStyle);

$conn = Mssql_connect ($config [' MSSQL '] [' host '], $config [' MSSQL ' [' User '], $config [' MSSQL '] [' password ']);
mssql_select_db ($config [' MSSQL '] [' dbname '], $conn);

$sql = "exec hnow05_getzzspace ', '". $tm. "', ' ', 1";
$sql =mb_convert_encoding ($sql, ' UTF-8 ', ' GBK ');
$res =mssql_query ($sql);

while ($arr = Mssql_fetch_array ($res)) {
$table->addrow ();
$table->addcell (->addtext) (Iconv (' GBK ', ' utf-8 ', $arr ["STCD"]);
$table->addcell (->addtext) (Iconv (' utf-8 ', ' GBK ', $arr ["STNM"]);
$table->addcell (->addtext) ($arr ["rvnm"]);
$table->addcell (2500)->addtext (iconv (' GBK ', ' utf-8 ', $arr ["TM"]));
$table->addcell (->addtext) (Iconv (' GBK ', ' utf-8 ', $arr ["Z"]));
$table->addcell (->addtext) (Iconv (' GBK ', ' utf-8 ', $arr ["WPTN"]);
if ($arr ["wptn"] = = 6) {
$table->addcell (->addtext) (Iconv (' utf-8 ', ' gbk ', ' flat '));
}else if ($arr ["wptn"] = = 5) {
$table->addcell (->addtext) (Iconv (' GBK ', ' utf-8 ', ' up '));
}else if ($arr ["wptn"] = = 4) {
$table->addcell (->addtext) (Iconv (' GBK ', ' utf-8 ', ' Fall '));
}
}
}

The various ways and means of transcoding above have been used, that is, No.

You converted GBK to Utf-8 in the template,
Will the SetValue method be called when addtext?

Not quite understand!!!

You converted GBK to Utf-8 in the template,
Will the SetValue method be called when addtext?
There is no call to SetValue () this method

string--array if you use the GBK, you have to convert the string to a group of arrays

//The reason for garbled is GBK at the time of conversion data type conversion: Character--array so garbled
foreach ($orders as $key 1 = $order)
{
$orders [$key 1][' order_goods '] = ';
if ($orders [$key 1][' Add_time '])
{$orders [$key 1][' add_time '] = Date (' y-m-d h:i:s ', $orders [$key 1][' Add_time '] );}
if ($orders [$key 1][' Pay_time '])
{$orders [$key 1][' pay_time '] = Date (' y-m-d h:i:s ', $orders [$key 1][' Pay_time '] );}
if ($orders [$key 1][' Ship_time '])
{$orders [$key 1][' ship_time '] = Date (' y-m-d h:i:s ', $orders [$key 1][' Ship_ Time ']);}
if ($orders [$key 1][' Finished_time '])
{$orders [$key 1][' finished_time '] = Date (' y-m-d h:i:s ', $orders [$key 1][') Finished_time ']);}
if ($orders [$key 1][' Evaluation_time '])
{$orders [$key 1][' evaluation_time '] = Date (' y-m-d h:i:s ', $orders [$ key1][' Evaluation_time ');}
}

The field in the database is a string, but if you export the data in GBK way, you have to convert the data type.
If the utf-8 is not needed.

Well, there's another way you can try, save your Program files in gb2312 format, read the data from the database
Iconv (' Utf-8 ', ' gb2312 ', $text);
And then add it to your Word document.

Or, sometimes garbled because of the OB buffer reason, before writing to the Word document with Ob_clean () to try:

Feel Phpword This library is almost the same as the Phpexcel library.

Try to unify the UTF-8 code

The cause of garbled is nothing more than the conversion of data types.
String--array if you use GBK, you have to convert the string to a group.

The reason for the garbled is the conversion of the GBK data type at the time of conversion: character--array so there was garbled
foreach ($orders as $key 1 = $order)
{
$orders [$key 1][' order_goods '] = ';
if ($orders [$ ...
are utf-8, including the database, in the design of the database is also used Utf-8

Feel Phpword This library is almost the same as the Phpexcel library.
Is the same, I used the phpexcel last week, the code is all converted to Utf-8, and then inserted into Excel when the success. Then I this time and Excel method to convert all the code into UFT-8, but it is garbled.

Well, there's another way you can try, save your Program files in gb2312 format, read the data from the database
Iconv (' Utf-8 ', ' gb2312 ', $text);
And then add it to your Word document.

Or, sometimes garbled because of the OB buffer reason, before writing to the Word document with Ob_clean () to try:
Just tried, the program file is saved as gb2312 format, or not, forcibly from Utf-8 to GBK, inserted into Word empty.
Ob_clean () also tried, did not respond

Just downloaded a Phpword test, you can enter Chinese (using a template to generate Word files).

My format is: Save the file in ANSI format

Then modify the template.php file,
Change the Utf8_encode line to $replace=iconv (' GBK ', ' utf-8 ', $replace);
After that, you can output Chinese, and it's not garbled.

Your database is encoded in UTF-8 format. Can do this:
1. If your program is also utf-8 format, it is recommended to put $replace=iconv (' GBK ', ' utf-8 ', $replace), also commented out, so that no transcoding.

2. If your program is GBK encoded, you need to Iconv (' utf-8 ', ' GBK ', $value) from the database and template. PHP $replace=iconv (' GBK ', ' utf-8 ', $replace);

Give it a try. Good luck

The pages that relate to your function are all added to the page display code, and the tool is changed to this code. But make sure that after the conversion of the code you look at the text when the language is not garbled, you will find it possible to fail him all become?????? , you delete the page and copy it back in. You can use notepad++, or editplus.
The database's code also changed, and then see for themselves is not garbled. The database reads the encoding to be set before the connection.
。。。。。。。
If you do this, you will generally not be garbled. If still garbled can only indicate that there are pages you have changed, or failed.
Do not be afraid of annoying, I have been on this for a long time.

The pages that relate to your function are all added to the page display code, and the tool is changed to this code. But make sure that after the conversion of the code you look at the text when the language is not garbled, you will find it possible to fail him all become?????? , you delete the page and copy it back in. You can use notepad++, or editplus.
The database's code also changed, and then see for themselves is not garbled. The database reads the encoding to be set before the connection.
。。。。。。。
If you do this, generally you will not ...
Oh, your method I have noticed, I have almost can transcode the place to turn into utf-8.

Just downloaded a Phpword test, you can enter Chinese (using a template to generate Word files).

My format is: Save the file in ANSI format

Then modify the template.php file,
Change the Utf8_encode line to $replace=iconv (' GBK ', ' utf-8 ', $replace);
After that, you can output Chinese, and it's not garbled.

Your database is encoded in UTF-8 format. Can do this:
1. If your program is UT ...
Alas, still failed, it seems to use the ultimate trick ....

This module overturned re-write again, hey new, I will follow your method step by step try, may be my mid-term some problems, thank you ah.

Thank you, guys.

I've tried a way to.
Find the AddText function of section.php, which will

$givenText = Utf8_encode ($text);

Change into
$givenText = Iconv (' GBK ', ' utf-8 ', $text);

Then Section->addtext (iconv (' utf-8 ', ' gbk ', ' your string ') is available, equivalent to converting to GBK before converting to utf-8.
Description: PHP file is already utf-8 encoded, do not know how to turn this can be

Thanks upstairs, I and landlord encounter the same problem.
Inspired by the upstairs, made a change, it succeeded.
Roughly summed up:
1.phpword has a template function, template.php this file
According to the landlord said,
Public Function SetValue ($search, $replace) {
if (substr ($search, 0, 2)!== ' ${' && substr ($search,-1)!== '} ') {
$search = ' ${'. $search. '} ';
}

if (!is_array ($replace)) {
$replace = Utf8_encode ($replace);
$replace =iconv (' GBK ', ' utf-8 ', $replace);
}
Replace the Utf8_encode inside with Iconv (' GBK ', ' utf-8 ', $replace);
When you replace a variable in Chinese with a template, you can solve the garbled problem

There is a table function in 2.phpword
To add a table when it's worth typing in Chinese:
$table->addcell ($styleCell)->addtext (' station code ', $fontStyle);
You need to modify the file: the AddText () method in phpword\section\table\cell.php
Public Function AddText ($text, $styleFont = null, $styleParagraph = null) {
$text = Utf8_encode ($text);
$text = new Phpword_section_text ($text, $styleFont, $styleParagraph);
$this->_elementcollection[] = $text;
return $text;
}
Take this line $text = Utf8_encode ($text); Comment it out.
There is a direct need to add Chinese text, according to the upstairs said.

As a summary, Phpword in the method of adding content, or replacing content, the input string is basically utf8_encode processed.
If the input encoding is a iso-8859-1 character set, you do not have to make any changes, if it is a different character set, you need to use this sentence with the Iconv function, change the character set to UTF8

Theoretically, you can re-enter the character set to iso-8859-1 directly, so that you do not need to move the background of the Phpword code.

Thanks upstairs, I and landlord encounter the same problem.
Inspired by the upstairs, made a change, it succeeded.
Roughly summed up:
1.phpword has a template function, template.php this file
According to the landlord said,
Public Function SetValue ($search, $replace) {
if (substr ($search, 0, 2)!== ' ${' && substr ($search,-1)!== '} ') {
$search = ' ${'. $search. '} ';
}

if (!is_array ($replace)) {
$replace = Utf8_encode ($replace);
$replace =iconv (' GBK ', ' utf-8 ', $replace);
}
Replace the Utf8_encode inside with Iconv (' GBK ', ' utf-8 ', $replace);
When you replace a variable in Chinese with a template, you can solve the garbled problem

There is a table function in 2.phpword
To add a table when it's worth typing in Chinese:
$table->addcell ($styleCell)->addtext (' station code ', $fontStyle);
You need to modify the file: the AddText () method in phpword\section\table\cell.php
Public Function AddText ($text, $styleFont = null, $styleParagraph = null) {
$text = Utf8_encode ($text);
$text = new Phpword_section_text ($text, $styleFont, $styleParagraph);
$this->_elementcollection[] = $text;
return $text;
}
Take this line $text = Utf8_encode ($text); Comment it out.
There is a direct need to add Chinese text, according to the upstairs said.

As a summary, Phpword in the method of adding content, or replacing content, the input string is basically utf8_encode processed.
If the input encoding is a iso-8859-1 character set, you do not have to make any changes, if it is a different character set, you need to use this sentence with the Iconv function, change the character set to UTF8

Theoretically, you can re-enter the character set to iso-8859-1 directly, so that you do not need to move the background of the Phpword code.

was educated.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.