utf8_encode

(PHP 4, PHP 5, PHP 7, PHP 8)

utf8_encode将字符串从 ISO-8859-1 转换为 UTF-8 编码

警告

此函数自 PHP 8.2.0 起弃用。强烈建议不要应用此函数。

说明

utf8_encode(string $string): string

该函数将 string 字符串从 ISO-8859-1 编码转换为 UTF-8

注意:

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 similar Windows-1252 encoding, and web browsers will interpret ISO-8859-1 web pages as Windows-1252. Windows-1252 features additional printable characters, such as the Euro sign () and curly quotes ( ), instead of certain ISO-8859-1 control characters. This function will not convert such Windows-1252 characters correctly. Use a different function if Windows-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.

范例

示例 #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

参见

  • utf8_decode() - 将字符串从 UTF-8 转换为 ISO-8859-1,替换无效或者无法表示的字符。
  • mb_convert_encoding() - 转换字符串,从一个字符编码到另一个字符编码
  • UConverter::transcode() - Convert a string from one character encoding to another
  • iconv() - 将字符串从一个字符编码转换到另一个字符编码

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top