3 * File containing the ezcMailCharsetConverter class.
7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
12 * Class containing common character set conversion methods.
14 * By calling the static function ezcMailCharsetConverter::setConvertMethod()
15 * before doing mail parsing, another callback function can be used for
16 * character conversion to UTF-8 in place of the normal iconv() conversion.
18 * The callback function must have this signature:
20 * public static function function_name( $text, $originalCharset );
24 * - $text = string to convert to UTF-8
25 * - $originalCharset = in what charset is $text
29 * // specify another function for character set conversion
30 * ezcMailCharsetConverter::setConvertMethod( array( 'myConverter', 'convertToUTF8IconvIgnore' ) );
32 * // ...code for mail parsing...
35 * where myConverter is (along with some other examples of charset conversion
36 * functions which can be used):
40 * public static function convertToUTF8IconvIgnore( $text, $originalCharset )
42 * if ( $originalCharset === 'unknown-8bit' || $originalCharset === 'x-user-defined' )
44 * $originalCharset = "latin1";
46 * return iconv( $originalCharset, 'utf-8//IGNORE', $text );
49 * public static function convertToUTF8IconvTranslit( $text, $originalCharset )
51 * if ( $originalCharset === 'unknown-8bit' || $originalCharset === 'x-user-defined' )
53 * $originalCharset = "latin1";
55 * return iconv( $originalCharset, 'utf-8//TRANSLIT', $text );
58 * public static function convertToUTF8Mbstring( $text, $originalCharset )
60 * return mb_convert_encoding( $text, "UTF-8", $originalCharset );
65 * Developers can choose to use the error suppresion operator ('@') in front of
66 * the iconv() calls in the above examples, in order to ignore the notices thrown
67 * when processing broken text (issue #8369).
72 class ezcMailCharsetConverter
75 * Callback function to use for character set conversion to UTF8.
79 private static $method = array( __CLASS__
, 'convertToUTF8Iconv' );
82 * Sets the callback function used for character set conversion to UTF-8.
84 * Call this method before doing mail parsing if you need a special way
85 * of converting the character set to UTF-8.
87 * @param callback $method
89 public static function setConvertMethod( $method )
91 self
::$method = $method;
95 * Converts the $text with the charset $originalCharset to UTF-8.
97 * It calls the function specified by using the static method
98 * setConvertMethod(). By default it calls convertToUTF8Iconv() defined
101 * @param string $text
102 * @param string $originalCharset
105 public static function convertToUTF8( $text, $originalCharset )
107 return call_user_func( self
::$method, $text, $originalCharset );
111 * Converts the $text with the charset $originalCharset to UTF-8.
113 * In case $originalCharset is 'unknown-8bit' or 'x-user-defined' then
114 * it is assumed to be 'latin1' (ISO-8859-1).
116 * @param string $text
117 * @param string $originalCharset
120 public static function convertToUTF8Iconv( $text, $originalCharset )
122 if ( $originalCharset === 'unknown-8bit' ||
$originalCharset === 'x-user-defined' )
124 $originalCharset = "latin1";
126 return iconv( $originalCharset, 'utf-8', $text );