(PHP 4, PHP 5, PHP 7, PHP 8)
utf8_encode — ½«×Ö·û´®´Ó ISO-8859-1 ת»»Îª UTF-8 ±àÂë
´Ëº¯Êý×Ô PHP 8.2.0 ÆðÆúÓá£Ç¿ÁÒ½¨Òé²»ÒªÓ¦Óô˺¯Êý¡£
¸Ãº¯Êý½« string
×Ö·û´®´Ó ISO-8859-1
±àÂëת»»Îª UTF-8
¡£
Note:
This function does not attempt to guess the current encoding of the provided string, it assumes it is encoded as ISO-8859-1 (also known as "Latin 1") and converts to UTF-8. Since every sequence of bytes is a valid ISO-8859-1 string, this never results in an error, but will not result in a useful string if a different encoding was intended.
Many web pages marked as using the
ISO-8859-1
character encoding actually use the similarWindows-1252
encoding, and web browsers will interpretISO-8859-1
web pages asWindows-1252
.Windows-1252
features additional printable characters, such as the Euro sign (€
) and curly quotes ("
"
), instead of certainISO-8859-1
control characters. This function will not convert suchWindows-1252
characters correctly. Use a different function ifWindows-1252
conversion is required.
string
ISO-8859-1 ×Ö·û´®¡£
·µ»Ø string
µÄ UTF-8 ·Òë¡£
°æ±¾ | ˵Ã÷ |
---|---|
8.2.0 | ÆúÓô˺¯Êý¡£ |
7.2.0 | This function has been moved from the XML extension to the core of PHP. In previous versions, it was only available if the XML extension was installed. |
Example #1 »ù´¡Ê¾Àý
<?php
// Convert the string 'Zo?' from ISO 8859-1 to UTF-8
$iso8859_1_string = "\x5A\x6F\xEB";
$utf8_string = utf8_encode($iso8859_1_string);
echo bin2hex($utf8_string), "\n";
?>
ÒÔÉÏʾÀý»áÊä³ö£º
5a6fc3ab
Note: ÆúÓúÍÌæ´ú·½°¸
´Ó PHP 8.2.0 ¿ªÊ¼£¬ÆúÓô˺¯Êý£¬²¢½«ÔÚδÀ´µÄ°æ±¾ÖÐɾ³ý¡£Ó¦¼ì²éÏÖÓÐÓÃ;²¢ÓÃÊʵ±µÄÌæ´ú·½°¸¡£
ÀàËÆµÄ¹¦ÄÜ¿ÉÒÔͨ¹ý mb_convert_encoding() ʵÏÖ£¬Ö§³Ö ISO-8859-1 ºÍÐí¶àÆäËû×Ö·û±àÂë¡£
<?php
$iso8859_1_string = "\xEB"; // '?' (e with diaeresis) in ISO-8859-1
$utf8_string = mb_convert_encoding($iso8859_1_string, 'UTF-8', 'ISO-8859-1');
echo bin2hex($utf8_string), "\n";
$iso8859_7_string = "\xEB"; // the same string in ISO-8859-7 represents '¦Ë' (Greek lower-case lambda)
$utf8_string = mb_convert_encoding($iso8859_7_string, 'UTF-8', 'ISO-8859-7');
echo bin2hex($utf8_string), "\n";
$windows_1252_string = "\x80"; // '€' (Euro sign) in Windows-1252, but not in ISO-8859-1
$utf8_string = mb_convert_encoding($windows_1252_string, 'UTF-8', 'Windows-1252');
echo bin2hex($utf8_string), "\n";
?>ÒÔÉÏʾÀý»áÊä³ö£º
c3ab cebb e282ac¸ù¾Ý°²×°µÄÀ©Õ¹£¬ÆäËûÓÐЧѡÏîÊÇ UConverter::transcode() ºÍ iconv()¡£
ÒÔ϶¼¸ø³öÏàͬµÄ½á¹û£º
<?php
$iso8859_1_string = "\x5A\x6F\xEB"; // 'Zo?' in ISO-8859-1
$utf8_string = utf8_encode($iso8859_1_string);
echo bin2hex($utf8_string), "\n";
$utf8_string = mb_convert_encoding($iso8859_1_string, 'UTF-8', 'ISO-8859-1');
echo bin2hex($utf8_string), "\n";
$utf8_string = UConverter::transcode($iso8859_1_string, 'UTF8', 'ISO-8859-1');
echo bin2hex($utf8_string), "\n";
$utf8_string = iconv('ISO-8859-1', 'UTF-8', $iso8859_1_string);
echo bin2hex($utf8_string), "\n";
?>ÒÔÉÏʾÀý»áÊä³ö£º
5a6fc3ab 5a6fc3ab 5a6fc3ab 5a6fc3ab