note 24791 added to function.utf8-encode
| From: | dimitrisATccfDOTauthDOTgr@php.net | Date: | Wed, 28 Aug 2002 15:04:53 +0000 |
| Subject: | note 24791 added to function.utf8-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-35918@lists.php.net to get a copy of this message | ||
To make utf8_encode and utf8_decode support other than iso-8859-1 encodings, you can easily define
your encoding in the PHP source.
In the file PHP_SOURCE/ext/xml/xml.c add the following code, for e.g. greek iso-8859-7:
DEFINE TWO NEW FUNCTIONS UP TOP:
inline static unsigned short xml_encode_iso_8859_7(unsigned char);
inline static char xml_decode_iso_8859_7(unsigned short);
AND THEN IMPLEMENT THEM BELOW:
/* {{{ xml_encode_iso_8859_7() - Dimitris Daskopoulos 28/8/02 */
/* map iso-8859-7 chars to Unicode chars */
inline static unsigned short xml_encode_iso_8859_7(unsigned char c)
{
if (c < 0x80) { /* low-ASCII, leave as is */
return (unsigned short)c;
} else { /* Greek character in high-ASCII */
/* map to UCS greek range (U+0310..03ff) */
/* assume that c < 0xff */
return (unsigned short)(c + 720);
}
}
/* }}} */
/* {{{ xml_decode_iso_8859_7() - Dimitris Daskopoulos 28/8/02 */
/* map Unicode chars to iso-8859-7 chars */
inline static char xml_decode_iso_8859_7(unsigned short c)
{
if (c < 0x100) { /* char in latin chart, leave as is */
return (char)c;
} else if (c > 0x030f && c < 0x0400) { /* char in greek chart */
/* map back to ISO-8859-7 greek (high-ASCII) */
return (char)(c - 720);
} else { /* char not in latin or greek Unicode charts */
/* return question mark character */
return (char)('?');
}
}
/* }}} */
These two work fine for greek iso-8859-7, but studying http://www.unicode.org/charts you
can implement mappings between unicode and other iso-8859-x quite easily.
In both functions (utf8_encode and utf8_decode), change the requested encoding to the one you
prefer, e.g.
encoded = xml_utf8_encode(Z_STRVAL_PP(arg), Z_STRLEN_PP(arg), &len, "ISO-8859-7");
decoded = xml_utf8_decode(Z_STRVAL_PP(arg), Z_STRLEN_PP(arg), &len, "ISO-8859-7");
Make sure you add the new encoding
in the structure, by entering a new
row with the official name (ISO-8859-7), and the names of the
two functions you have just defined:
xml_encoding xml_encodings[] = {
{ "ISO-8859-1", xml_decode_iso_8859_1, xml_encode_iso_8859_1 },
{ "US-ASCII", xml_decode_us_ascii, xml_encode_us_ascii },
{ "UTF-8", NULL, NULL },
{ "ISO-8859-7", xml_decode_iso_8859_7, xml_encode_iso_8859_7 },
{ NULL, NULL, NULL }
};
Finally, the following is probably not necessary, but I changed the default encoding (found in 2
spots in this file) to whatever encoding you prefer in your
pages, e.g.:
XML(default_encoding) = "ISO-8859-7";
This solution is a little messy,
since the utf8_encode function does not accept an argument for choosing the encoding method to use
but hardwires the encoding method in the source code. Maybe PHP developers will provide this option
in future releases. Until then, this is a quick and dirty solution that will work for
localized PHP pages.
Dimitris Daskopoulos
--
http://www.php.net/manual/en/function.utf8-encode.php
http://master.php.net/manage/user-notes.php?action=edit+24791
http://master.php.net/manage/user-notes.php?action=delete+24791
http://master.php.net/manage/user-notes.php?action=reject+24791