note 56477 rejected from function.mb-encode-mimeheader by nlopess
| From: | nlopess@php.net | Date: | Sun, 04 Sep 2005 08:36:29 +0000 |
| Subject: | note 56477 rejected from function.mb-encode-mimeheader by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94716@lists.php.net to get a copy of this message | ||
Note Submitter: atnak at chejz dot com
----
Version 4.3.1
This function does have at least one bug, but the problem of splits occurring in between multibyte
character bytes may only be a side-effect of a much simpler problem:
It seems that the second parameter of this function 'charset ' doesn't do anything
*at all*.
Background:
I too have noticed that strings encoded with this function comes out wrong if they are too long.
(i.e. decode(encode()) != original)
The following illustrates a possible scenario:
<?php
mb_internal_encoding('SJIS');
$str = 'A Shift-JIS string that is overall longer than 76 bytes';
$str = mb_convert_encoding($str, 'ISO-2022-JP');
$str = mb_encode_mimeheader($str, 'ISO-2022-JP');
?>
The value of $str comes out wrong and, after some testing, it is apparent multibyte characters are
broken up in between bytes. A wrapper function for mb_encode_mimeheader() that manually makes the
inputs shorter and concatenates the results with a 'CRLF ' delimiter fixes the problem.
However, it now seems that the only reason multibyte characters are broken up in between bytes is
because mb_encode_mimeheader() is interpreting the 'ISO-2022-JP' string as
'SJIS'.
I've found that the following, all-too-simple, workaround fixes the splitting problem, as well
as a garbling issue encountered when internal = SJIS, converted = UTF-8.
<?php
mb_encode_mimeheader_wrapper($str, $charset, ...)
{
// Save and switch.
$internal_encoding = mb_internal_encoding();
mb_internal_encoding($str, $charset);
// Perform.
$encoded = mb_encode_mimeheader($str, $charset, ...);
// Restore.
mb_internal_encoding($internal_encoding);
return $encoded;
}
?>
[Omissions in the code have been made for simplicity.]