mirror of
https://github.com/php/doc-ru.git
synced 2026-03-24 07:42:22 +01:00
* Update date-sunrise.xml * Update date-sunset.xml * Update date-sunrise.xml * Update gmstrftime.xml * Update strftime.xml * Update strptime.xml * Update enchant-broker-free-dict.xml * Update enchant-broker-free.xml * Update enchant-broker-get-dict-path.xml * Update enchant-broker-set-dict-path.xml * Update libxml-disable-entity-loader.xml * Update mhash-count.xml * Update mhash-get-block-size.xml * Update mhash-get-hash-name.xml * Update mhash-keygen-s2k.xml * Update mhash.xml * Update get-client-info.xml * Update init.xml * Update kill.xml * Update ping.xml * Update refresh.xml * Update openssl-free-key.xml * Update openssl-pkey-free.xml * Update openssl-x509-free.xml * Update lcg-value.xml * Update isdisabled.xml * Update getclass.xml * Update isarray.xml * Update isarray.xml amend the code example * Update iscallable.xml * Update shmop-close.xml * Update shmop-close.xml Add the missing block * Update wakeup.xml * Update utf8-decode.xml * Update utf8-decode.xml * Update utf8-encode.xml * Update utf8-decode.xml * Update odbc-result-all.xml * Update xml-set-object.xml * Update zip-close.xml * Update zip-entry-close.xml * Update zip-entry-compressedsize.xml * Update zip-entry-compressionmethod.xml * Update zip-entry-filesize.xml * Update zip-entry-name.xml * Update zip-entry-open.xml * Update zip-entry-read.xml * Update zip-open.xml * Update zip-read.xml
285 lines
9.6 KiB
XML
285 lines
9.6 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- EN-Revision: 9b1673cf114a1e10c4563ab9223cb56aed552b89 Maintainer: sergey Status: ready -->
|
||
<!-- Reviewed: no -->
|
||
<refentry xml:id="function.utf8-decode" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>utf8_decode</refname>
|
||
<refpurpose>
|
||
Преобразовывает строку из кодировки UTF-8 в кодировку ISO-8859-1, заменяя недопустимые или непредставимые символы
|
||
</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsynopsisdiv>
|
||
&warn.deprecated.function-8-2-0;
|
||
</refsynopsisdiv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<modifier role="attribute">#[\Deprecated]</modifier>
|
||
<type>string</type><methodname>utf8_decode</methodname>
|
||
<methodparam><type>string</type><parameter>string</parameter></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
Функция преобразовывает строку <parameter>string</parameter> из кодировки
|
||
<literal>UTF-8</literal> в кодировку <literal>ISO-8859-1</literal>.
|
||
Байты в строке, которые не соответствуют корректным символам кодировки <literal>UTF-8</literal>
|
||
и <literal>UTF-8</literal>-символам, которые не содержатся в кодировке <literal>ISO-8859-1</literal> —
|
||
кодовые точки выше <literal>U+00FF</literal>, заменяются на символ <literal>?</literal>.
|
||
</para>
|
||
|
||
<note>
|
||
<para>
|
||
Часто веб-страницы, которые отметили как страницы в кодировке
|
||
<literal>ISO-8859-1</literal>, кодируются похожей кодировкой —
|
||
<literal>Windows-1252</literal>, и браузеры интерпретируют
|
||
страницы в кодировке <literal>ISO-8859-1</literal> как страницы в кодировке <literal>Windows-1252</literal>.
|
||
Однако кодировка <literal>Windows-1252</literal> вместо управляющих кодов
|
||
кодировки <literal>ISO-8859-1</literal> содержит дополнительные печатные
|
||
символы наподобие знака евро <literal>€</literal> и английских двойных кавычек
|
||
<literal>“</literal> <literal>”</literal>. Функция не конвертирует такие символы
|
||
кодировки <literal>Windows-1252</literal> корректно. Для конвертации в кодировку
|
||
<literal>Windows-1252</literal> пользуются альтернативными функциями.
|
||
</para>
|
||
</note>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>string</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
Строка в кодировке UTF-8.
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
Функция возвращает данные параметра <parameter>string</parameter>, которые преобразовала в кодировку ISO-8859-1.
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="changelog">
|
||
&reftitle.changelog;
|
||
<para>
|
||
<informaltable>
|
||
<tgroup cols="2">
|
||
<thead>
|
||
<row>
|
||
<entry>&Version;</entry>
|
||
<entry>&Description;</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry>8.2.0</entry>
|
||
<entry>
|
||
Функция устарела.
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry>7.2.0</entry>
|
||
<entry>
|
||
Функцию перенесли из модуля XML в ядро PHP.
|
||
В предыдущих версиях функция была доступна только при установленном модуле XML.
|
||
</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</informaltable>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<example>
|
||
<title>Простой пример преобразования строки из кодировки UTF-8 в кодировку ISO-8859-1</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
// Преобразование строки 'Zoë' из кодировки UTF-8 в кодировку ISO 8859-1
|
||
$utf8_string = "\x5A\x6F\xC3\xAB";
|
||
$iso8859_1_string = utf8_decode($utf8_string);
|
||
echo bin2hex($iso8859_1_string), "\n";
|
||
|
||
// Неправильные для кодировки UTF-8 последовательности заменяются на '?'
|
||
$invalid_utf8_string = "\xC3";
|
||
$iso8859_1_string = utf8_decode($invalid_utf8_string);
|
||
var_dump($iso8859_1_string);
|
||
|
||
// Символы, которые не содержатся в кодировке ISO 8859-1 наподобие
|
||
// знака евро '€', тоже заменяются символом '?'
|
||
$utf8_string = "\xE2\x82\xAC";
|
||
$iso8859_1_string = utf8_decode($utf8_string);
|
||
var_dump($iso8859_1_string);
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
5a6feb
|
||
string(1) "?"
|
||
string(1) "?"
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</refsect1>
|
||
|
||
<refsect1 role="notes">
|
||
&reftitle.notes;
|
||
<note>
|
||
<title>Устаревание и альтернативы</title>
|
||
<para>
|
||
Начиная с PHP 8.2.0 функция <emphasis>устарела</emphasis> и её удалят в будущей версии.
|
||
Разработчики языка рекомендуют заменить вызовы функции в коде альтернативами.
|
||
</para>
|
||
<para>
|
||
Аналогичную функциональность даёт функция <function>mb_convert_encoding</function>,
|
||
которая поддерживает кодировку ISO-8859-1 и набор других кодировок символов.
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$utf8_string = "\xC3\xAB"; // 'ë' — буква «e» с диерезисом в кодировке 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"; // 'λ' — греческая строчная лямбда в кодировке 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"; // '€' — символ евро в кодировке UTF-8, не содержится в кодировке ISO-8859-1
|
||
$windows_1252_string = mb_convert_encoding($utf8_string, 'Windows-1252', 'UTF-8');
|
||
echo bin2hex($windows_1252_string), "\n";
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
eb
|
||
eb
|
||
80
|
||
]]>
|
||
</screen>
|
||
</informalexample>
|
||
</para>
|
||
<para>
|
||
Другие способы, доступность которых зависит от загруженных модулей, —
|
||
метод <methodname>UConverter::transcode</methodname> и функция <function>iconv</function>.
|
||
</para>
|
||
<para>
|
||
Каждый следующий способ даёт один и тот же результат:
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$utf8_string = "\x5A\x6F\xC3\xAB"; // 'Zoë' в кодировке 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";
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
5a6feb
|
||
5a6feb
|
||
5a6feb
|
||
5a6feb
|
||
]]>
|
||
</screen>
|
||
</informalexample>
|
||
Символ <literal>'?'</literal> как значение элемента <literal>'to_subst'</literal> в массиве опций
|
||
метода <methodname>UConverter::transcode</methodname> даёт тот же результат,
|
||
что и функция <function>utf8_decode</function> для недопустимых строк
|
||
или строк, которые невозможно представить в кодировке ISO 8859-1.
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$utf8_string = "\xE2\x82\xAC"; // € — символ евро, отсутствует в кодировке ISO-8859-1
|
||
|
||
$iso8859_1_string = UConverter::transcode(
|
||
$utf8_string,
|
||
'ISO-8859-1',
|
||
'UTF-8',
|
||
['to_subst' => '?']
|
||
);
|
||
|
||
var_dump($iso8859_1_string);
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
sring(1) "?"
|
||
]]>
|
||
</screen>
|
||
</informalexample>
|
||
</para>
|
||
</note>
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><function>utf8_encode</function></member>
|
||
<member><function>mb_convert_encoding</function></member>
|
||
<member><methodname>UConverter::transcode</methodname></member>
|
||
<member><function>iconv</function></member>
|
||
</simplelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
</refentry>
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|