Illegal Character Encoding Error

Jul 1, 2009 at 12:07 PM
Edited Jul 1, 2009 at 12:28 PM

On a sheet that I can't share, and can't figure out what portion of the sheet makes it happen, I get this error over and over:

Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233

It doesn't happen when I save the sheet with OpenOffice, but does happen with Mac Excel.

On this bug, I can say that 2.6.5 did not have the problem, but 2.6.7 does. (I did not try 2.6.6.)  I will post more if I can figure out why this happens.

Developer
Jul 1, 2009 at 1:01 PM

For the sake of debugging, can you try to change that line 233 in PHPExcel/Shared/String.php from this

$value = mb_convert_encoding($value, $to, $from);

to this

var_dump($value, $to, $from); $value = mb_convert_encoding($value, $to, $from);

and report back what it dumps?

Jul 1, 2009 at 1:08 PM

string(9) "Worksheet" string(5) "UTF-8" string(0) ""
Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233
string(12) "Calculations" string(5) "UTF-8" string(0) ""
Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233
string(10) "Percentage" string(5) "UTF-8" string(0) ""
Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233
string(3) "Sat" string(5) "UTF-8" string(0) ""
Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233
string(8) "Addition" string(5) "UTF-8" string(0) ""
Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233
string(3) "Sun" string(5) "UTF-8" string(0) ""
Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233
string(3) "Mon" string(5) "UTF-8" string(0) ""
Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233
string(3) "Tue" string(5) "UTF-8" string(0) ""
Warning: mb_convert_encoding() [function.mb-convert-encoding]: Illegal character encoding specified in /mnt/hgfs/svn/ezyield_v3/library/D2EApplication/_packages/PHPExcel/Shared/String.php on line 233

It looks like every text string on the sheet is listed, but I've only given a sample here.

Developer
Jul 1, 2009 at 2:09 PM

Thanks, this is beginning to make sense. Looks like the Excel file is BIFF5, there are apparently some problematic character sets on Mac that are not supported by the conversion functions available in PHP. At least that is what I can read in the code comments.

Can you try to locate this line within _readCodepage in PHPExcel/Reader/Excel5.php :

switch ($codepage) {

and replace it with

var_dump($codepage); switch ($codepage) {

and report back the result.

Jul 1, 2009 at 3:54 PM

Ah.  Hmm...

int(10000)

So, 'Apple Roman', which isn't supported by iconv apparently.

Developer
Jul 1, 2009 at 4:09 PM

I just checked that. It appears after all to be supported by iconv. I think it may not have been supported previously.

You can view supported character sets by typing this at the command prompt:
iconv -l

Now, the problem is that PHPExcel is using mbstring as primary library to handle character set conversions. But mbstring does not support Mac OS Roman:
http://php.net/manual/en/mbstring.supported-encodings.php

Your example tells us that we must update PHPExcel to give priority to iconv over mbsring. Will fix this today.

Jul 1, 2009 at 4:20 PM

Awesome!  Thanks!

Developer
Jul 1, 2009 at 5:00 PM

Get patch here:
http://phpexcel.codeplex.com/WorkItem/View.aspx?WorkItemId=10229

Or just wait and download source code release tomorrow:
http://phpexcel.codeplex.com/SourceControl/ListDownloadableCommits.aspx

May 19, 2011 at 4:37 PM

I just downloaded the latest source code, which you supplied in the second link. However, the error that wccrawford names does still occur when I process an Excel xls created on a Mac.

May 19, 2011 at 4:55 PM

I just wanted to reiterate my thanks for the quick fix 2 years ago, and state that it did indeed solve my particular problem at the time.  

Unfortunately, I'm not in a position to test whether the bug is back, or it's something else this time.  (I no longer have access to that code and materials.)

I wasn't sure if that information would be useful for solving Curunir's problem, so I thought I'd post it just in case.

Good luck!

Aug 15, 2012 at 3:01 PM
Edited Aug 15, 2012 at 3:01 PM

I'm still getting

"mb_convert_encoding(): Illegal character encoding specified - /.../PHPExcel/Classes/PHPExcel/Shared/String.php (501)"

in version v1.7.7

 

Any ideas?

 

Thanks in advance,

Alex

Coordinator
Aug 15, 2012 at 5:51 PM

Suggests that either the fromEncoding value isn't correct for the encoding of the string, or that the toEncoding doesn't support one of the characters in the string, or that your system's libmbfl doesn't support the encoding. Can you try putting some debug statements in to determine what encodings are being converted to what?

Aug 16, 2012 at 9:58 AM
Edited Aug 16, 2012 at 10:13 AM

In ConvertEncoding($value, $to, $from):

$value is "Ex-Europe 2013"

$to is "UTF-8"
$from is "MAC"

Note: "iconv -l" lists both these.

Note: $ port list | grep mbstring
php54-mbstring                 @5.4.5          lang/php
php53-mbstring                 @5.3.15         lang/php
php5-mbstring                  @5.3.15         php/php5-mbstring

The last one is being used I think:

$ php --version
PHP 5.3.15 (cli) (built: Aug 16 2012 11:04:32)

Aug 16, 2012 at 3:05 PM

Ok, so I tried to switch my system to use php54 and that screwed everything up and I couldn't go back to an older version; so in the end I ended up deleting everything php in macports and starting over.

And.. now it works! Aparently there was some conflict or too-old version of mbstring running or something?

Anyway, thanks.

Sep 1, 2012 at 7:44 PM

It is ridiculous. I found that if you just want to read the data. Solution is

Use Open Office to open the xls, and change the font to any font for the whole sheet, then you will be able to read the data.

...

Sep 3, 2012 at 2:48 PM

Aaaand it's back :)

DEBUG - 2012-09-03 15:44:35 --> mb_convert_encoding->From: MAC To: UTF-8
DEBUG - 2012-09-03 15:44:35 --> mb_convert_encoding(): Illegal character encoding specified - /Users/alex/src/osa/application/libraries/PHPExcel/Classes/PHPExcel/Shared/String.php (502)

Really weird, didn't do anything to my config. Only thing I can think of is that the machine rebooted since last time I tried?