Mail: ezcMailCharsetConverter
[ ]
[ Display example ] [ Mail listing example ] [ Rfcs ]
[ ]
[ ]
[ ]
[ ]
Class: ezcMailCharsetConverter
|
Class containing common character set conversion methods. [
source]
By calling the static function ezcMailCharsetConverter::setConvertMethod() before doing mail parsing, another callback function can be used for character conversion to UTF-8 in place of the normal iconv() conversion.
The callback function must have this signature:
1. public static function function_name( $text, $originalCharset );
where:
- $text = string to convert to UTF-8
- $originalCharset = in what charset is $text
Example:
1. // specify another function for character set conversion
2. ezcMailCharsetConverter::setConvertMethod( array( 'myConverter', 'convertToUTF8IconvIgnore' ) );
3.
3. // ...code for mail parsing...
where myConverter is (along with some other examples of charset conversion functions which can be used):
1. class myConverter
2. {
3. public static function convertToUTF8IconvIgnore( $text, $originalCharset )
4. {
5. if ( $originalCharset === 'unknown-8bit' || $originalCharset === 'x-user-defined' )
6. {
7. $originalCharset = "latin1";
8. }
9. return iconv( $originalCharset, 'utf-8//IGNORE', $text );
10. }
11.
12. public static function convertToUTF8IconvTranslit( $text, $originalCharset )
13. {
14. if ( $originalCharset === 'unknown-8bit' || $originalCharset === 'x-user-defined' )
15. {
16. $originalCharset = "latin1";
17. }
18. return iconv( $originalCharset, 'utf-8//TRANSLIT', $text );
19. }
20.
21. public static function convertToUTF8Mbstring( $text, $originalCharset )
22. {
23. return mb_convert_encoding( $text, "UTF-8", $originalCharset );
24. }
25. }
Developers can choose to use the error suppresion operator ('@') in front of the iconv() calls in the above examples, in order to ignore the notices thrown when processing broken text (issue
#8369).
Method Summary
|
public static string |
convertToUTF8(
$text, $originalCharset )
Converts the $text with the charset $originalCharset to UTF-8. |
|
public static string |
convertToUTF8Iconv(
$text, $originalCharset )
Converts the $text with the charset $originalCharset to UTF-8. |
|
public static void |
setConvertMethod(
$method )
Sets the callback function used for character set conversion to UTF-8. |
Methods
convertToUTF8
string convertToUTF8(
string
$text, string
$originalCharset )
Converts the $text with the charset $originalCharset to UTF-8.
It calls the function specified by using the static method setConvertMethod(). By default it calls convertToUTF8Iconv() defined in this class.
Parameters
| Name |
Type |
Description |
$text |
string |
|
$originalCharset |
string |
|
convertToUTF8Iconv
string convertToUTF8Iconv(
string
$text, string
$originalCharset )
Converts the $text with the charset $originalCharset to UTF-8.
In case $originalCharset is 'unknown-8bit' or 'x-user-defined' then it is assumed to be 'latin1' (ISO-8859-1).
Parameters
| Name |
Type |
Description |
$text |
string |
|
$originalCharset |
string |
|
setConvertMethod
void setConvertMethod(
callback
$method )
Sets the callback function used for character set conversion to UTF-8.
Call this method before doing mail parsing if you need a special way of converting the character set to UTF-8.
Parameters
| Name |
Type |
Description |
$method |
callback |
|
Last updated: Tue, 06 Jan 2009