note 56477 added to function.mb-encode-mimeheader

From: Date: Sun, 04 Sep 2005 03:54:17 +0000
Subject: note 56477 added to function.mb-encode-mimeheader
Groups: php.notes 
Request: Send a blank email to php-notes+get-94709@lists.php.net to get a copy of this message
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.] ---- Manual Page -- http://www.php.net/manual/en/function.mb-encode-mimeheader.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56477 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56477&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56477&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56477&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56477&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56477&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56477&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56477&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94709) next »