note 24747 deleted from function.utf8-decode by cmb
| From: | cmb@php.net | Date: | Fri, 15 May 2015 19:57:25 +0000 |
| Subject: | note 24747 deleted from function.utf8-decode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-202565@lists.php.net to get a copy of this message | ||
Note Submitter:
----
echo utf8_decode(
"Ekonomi_ve_\xc4\xb0\xc5\x9f_D\xc3xbcnyas\xc4\xb1");
Outputs: Ekonomi_ve_??_Dünyas?
This is not a bug. For example "\xc4\xb0" is decoded like this:
0xC4 = 110 00100 (110 prefix for 2 bytes)
0xB0 = 10 110000 (10 for trailing byte)
Character = 001 0011 0000 = 0x130
This U+0130 Unicode character is not defined in ISO-8859-1
(where all codes are below 0x100), so the utf8_decode() function
cannot represent it, and inserts a '?' replacement character
in the result string...
There's no alternative in the utf8_decode() function
which returns a string made of
single-byte characters.
PHP really lacks a datatype for UTF-16 (a.k.a. Unicode) strings,
unlike Java whose strings are always natively encoded
with UTF-16...
The alternative for PHP would be that it allows inserting SGML
character entities. In the above example, it could return
"İ" when decoding "\xc4\xb0".
May be a future version of PHP could make strings using
double-byte UTF-16 encoding, so that Unicode could be natively
supported, but this would require adding new functions to
define the behavior of I/O functions to provide external
encode/decode capabilities when sending Unicode strings to
a file stream, or with echo and print builtins for the
standard input and output streams:
- What would be the semantic of fwrite($fp, $string)
if $string can contain any UTF-16 characters ?
- Should fwrite() truncate the highest byte of each UTF-16
character, letting the user anticipating this truncation by
performing the conversion to UTF-8 or character entities himself ?
- Should the user be able to specify an encoder/decoder object
used by all I/O operations performed on a file ?
For example:
- set_encoder($fp, 'utf8_encode') would define the PHP function
to call to encode a string.
- set_encoder($fp) or set_encoder($fp, null) would restore the
default (stripping) encoder...
- then fwrite($fp, $string) would return the number of bytes
written to the stream, which may be larger than the length of
$string...