reference/strings/functions/utf8-encode.xml
b676d0b9c968ebb0b7429835f2f6bd364275eed1
...
...
@@ -6,6 +6,10 @@
6
6
<refpurpose>Converts a string from ISO-8859-1 to UTF-8</refpurpose>
7
7
</refnamediv>
8
8

9
+
<refsynopsisdiv>
10
+
&warn.deprecated.function-8-2-0;
11
+
</refsynopsisdiv>
12
+

9
13
<refsect1 role="description">
10
14
&reftitle.description;
11
15
<methodsynopsis>
...
...
@@ -76,6 +80,12 @@
76
80
</thead>
77
81
<tbody>
78
82
<row>
83
+
<entry>8.2.0</entry>
84
+
<entry>
85
+
This function has been deprecated.
86
+
</entry>
87
+
</row>
88
+
<row>
79
89
<entry>7.2.0</entry>
80
90
<entry>
81
91
This function has been moved from the XML extension to the core of PHP.
...
...
@@ -111,6 +121,86 @@ echo bin2hex($utf8_string), "\n";
111
121
</example>
112
122
</refsect1>
113
123

124
+
<refsect1 role="notes">
125
+
&reftitle.notes;
126
+
<note>
127
+
<title>Deprecation and alternatives</title>
128
+
<para>
129
+
This function is <emphasis>deprecated</emphasis> as of PHP 8.2.0,
130
+
and will be removed in a future version. Existing uses should be checked
131
+
and replaced with appropriate alternatives.
132
+
</para>
133
+
<para>
134
+
Similar functionality can be achieved with <function>mb_convert_encoding</function>,
135
+
which supports ISO-8859-1 and many other character encodings.
136
+
<informalexample>
137
+
<programlisting role="php">
138
+
<![CDATA[
139
+
<?php
140
+
$iso8859_1_string = "\xEB"; // 'ë' (e with diaeresis) in ISO-8859-1
141
+
$utf8_string = mb_convert_encoding($iso8859_1_string, 'UTF-8', 'ISO-8859-1');
142
+
echo bin2hex($utf8_string), "\n";
143
+

144
+
$iso8859_7_string = "\xEB"; // the same string in ISO-8859-7 represents 'λ' (Greek lower-case lambda)
145
+
$utf8_string = mb_convert_encoding($iso8859_7_string, 'UTF-8', 'ISO-8859-7');
146
+
echo bin2hex($utf8_string), "\n";
147
+

148
+
$windows_1252_string = "\x80"; // '€' (Euro sign) in Windows-1252, but not in ISO-8859-1
149
+
$utf8_string = mb_convert_encoding($windows_1252_string, 'UTF-8', 'Windows-1252');
150
+
echo bin2hex($utf8_string), "\n";
151
+
?>
152
+
]]>
153
+
</programlisting>
154
+
&example.outputs;
155
+
<screen>
156
+
<![CDATA[
157
+
c3ab
158
+
cebb
159
+
e282ac
160
+
]]>
161
+
</screen>
162
+
</informalexample>
163
+
</para>
164
+
<para>
165
+
Other options which may be available depending on the extensions installed are
166
+
<methodname>UConverter::transcode</methodname> and <function>iconv</function>.
167
+
</para>
168
+
<para>
169
+
The following all give the same result:
170
+
<informalexample>
171
+
<programlisting role="php">
172
+
<![CDATA[
173
+
<?php
174
+
$iso8859_1_string = "\x5A\x6F\xEB"; // 'Zoë' in ISO-8859-1
175
+

176
+
$utf8_string = utf8_encode($iso8859_1_string);
177
+
echo bin2hex($utf8_string), "\n";
178
+

179
+
$utf8_string = mb_convert_encoding($iso8859_1_string, 'UTF-8', 'ISO-8859-1');
180
+
echo bin2hex($utf8_string), "\n";
181
+

182
+
$utf8_string = UConverter::transcode($iso8859_1_string, 'UTF8', 'ISO-8859-1');
183
+
echo bin2hex($utf8_string), "\n";
184
+

185
+
$utf8_string = iconv('ISO-8859-1', 'UTF-8', $iso8859_1_string);
186
+
echo bin2hex($utf8_string), "\n";
187
+
?>
188
+
]]>
189
+
</programlisting>
190
+
&example.outputs;
191
+
<screen>
192
+
<![CDATA[
193
+
5a6fc3ab
194
+
5a6fc3ab
195
+
5a6fc3ab
196
+
5a6fc3ab
197
+
]]>
198
+
</screen>
199
+
</informalexample>
200
+
</para>
201
+
</note>
202
+
</refsect1>
203
+

114
204
<refsect1 role="seealso">
115
205
&reftitle.seealso;
116
206
<para>
117
207