(PHP 4, PHP 5, PHP 7, PHP 8)
utf8_decode — ½«×Ö·û´®´Ó UTF-8 ת»»Îª ISO-8859-1£¬Ìæ»»ÎÞЧ»òÕßÎÞ·¨±íʾµÄ×Ö·û¡£
´Ëº¯Êý×Ô PHP 8.2.0 ÆðÆúÓá£Ç¿ÁÒ½¨Òé²»ÒªÓ¦Óô˺¯Êý¡£
¸Ãº¯Êý½«×Ö·û´® string
´Ó UTF-8
±àÂëת»»Îª
ISO-8859-1
¡£×Ö·û´®Öв»ÊÇÓÐЧ UTF-8 ×Ö½Ú»òÕß²»´æÔÚÓÚ ISO-8859-1
µÄ UTF-8
×Ö·û£¨¼´ U+00FF
ÒÔÉϵÄÂëµã£©½«×ª»¯Îª ?
¡£
Note:
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
UTF-8 ±àÂëµÄ×Ö·û´®¡£
·µ»Ø string
µÄ ISO-8859-1 ·Òë¡£
°æ±¾ | ˵Ã÷ |
---|---|
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 UTF-8 to ISO 8859-1
$utf8_string = "\x5A\x6F\xC3\xAB";
$iso8859_1_string = utf8_decode($utf8_string);
echo bin2hex($iso8859_1_string), "\n";
// Invalid UTF-8 sequences are replaced with '?'
$invalid_utf8_string = "\xC3";
$iso8859_1_string = utf8_decode($invalid_utf8_string);
var_dump($iso8859_1_string);
// Characters which don't exist in ISO 8859-1, such as
// '€' (Euro Sign) are also replaced with '?'
$utf8_string = "\xE2\x82\xAC";
$iso8859_1_string = utf8_decode($utf8_string);
var_dump($iso8859_1_string);
?>
ÒÔÉÏʾÀý»áÊä³ö£º
5a6feb string(1) "?" string(1) "?"
Note: ÆúÓúÍÌæ´ú·½°¸
´Ó PHP 8.2.0 ¿ªÊ¼£¬ÆúÓô˺¯Êý£¬²¢½«ÔÚδÀ´µÄ°æ±¾ÖÐɾ³ý¡£Ó¦¼ì²éÏÖÓÐÓÃ;²¢ÓÃÊʵ±µÄÌæ´ú·½°¸¡£
ÀàËÆµÄ¹¦ÄÜ¿ÉÒÔͨ¹ý mb_convert_encoding() ʵÏÖ£¬Ö§³Ö ISO-8859-1 ºÍÐí¶àÆäËû×Ö·û±àÂë¡£
<?php
$utf8_string = "\xC3\xAB"; // '?' (e with diaeresis) in UTF-8
$iso8859_1_string = mb_convert_encoding($utf8_string, 'ISO-8859-1', 'UTF-8');
echo bin2hex($iso8859_1_string), "\n";
$utf8_string = "\xCE\xBB"; // '¦Ë' (Greek lower-case lambda) in UTF-8
$iso8859_7_string = mb_convert_encoding($utf8_string, 'ISO-8859-7', 'UTF-8');
echo bin2hex($iso8859_7_string), "\n";
$utf8_string = "\xE2\x82\xAC"; // '€' (Euro sign) in UTF-8 (not present in ISO-8859-1)
$windows_1252_string = mb_convert_encoding($utf8_string, 'Windows-1252', 'UTF-8');
echo bin2hex($windows_1252_string), "\n";
?>ÒÔÉÏʾÀý»áÊä³ö£º
eb eb 80¸ù¾Ý°²×°µÄÀ©Õ¹£¬ÆäËûÓÐЧѡÏîÊÇ UConverter::transcode() ºÍ iconv()¡£
ÒÔ϶¼¸ø³öÏàͬµÄ½á¹û£º
Õë¶ÔÎÞЧµÄ×Ö·û´®»òÕß²»ÄÜÓà ISO 8859-1 ±íʾµÄ×Ö·û´®£¬Ö¸¶¨<?php
$utf8_string = "\x5A\x6F\xC3\xAB"; // 'Zo?' in UTF-8
$iso8859_1_string = utf8_decode($utf8_string);
echo bin2hex($iso8859_1_string), "\n";
$iso8859_1_string = mb_convert_encoding($utf8_string, 'ISO-8859-1', 'UTF-8');
echo bin2hex($iso8859_1_string), "\n";
$iso8859_1_string = iconv('UTF-8', 'ISO-8859-1', $utf8_string);
echo bin2hex($iso8859_1_string), "\n";
$iso8859_1_string = UConverter::transcode($utf8_string, 'ISO-8859-1', 'UTF8');
echo bin2hex($iso8859_1_string), "\n";
?>ÒÔÉÏʾÀý»áÊä³ö£º
5a6feb 5a6feb 5a6feb 5a6feb'?'
×÷Ϊ UConverter::transcode() µÄ'to_subst'
Ñ¡Ï½«»ñµÃͬ utf8_decode() ÏàͬµÄ½á¹û¡£<?php
$utf8_string = "\xE2\x82\xAC"; // € (Euro Sign) does not exist in ISO 8859-1
$iso8859_1_string = UConverter::transcode(
$utf8_string, 'ISO-8859-1', 'UTF-8', ['to_subst' => '?']
);
var_dump($iso8859_1_string);
?>ÒÔÉÏʾÀý»áÊä³ö£º
sring(1) "?"