mirror of
https://github.com/php/doc-fr.git
synced 2026-03-24 15:12:13 +01:00
285 lines
8.9 KiB
XML
285 lines
8.9 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- EN-Revision: 7c4b5fb40ac3149a5b931f1e31b1050ab5eaab7e Maintainer: girgias Status: ready -->
|
||
<!-- Reviewed: no -->
|
||
<refentry xml:id="function.mb-detect-encoding" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||
<refnamediv>
|
||
<refname>mb_detect_encoding</refname>
|
||
<refpurpose>Détecte un encodage</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type class="union"><type>string</type><type>false</type></type><methodname>mb_detect_encoding</methodname>
|
||
<methodparam><type>string</type><parameter>string</parameter></methodparam>
|
||
<methodparam choice="opt"><type class="union"><type>array</type><type>string</type><type>null</type></type><parameter>encodings</parameter><initializer>&null;</initializer></methodparam>
|
||
<methodparam choice="opt"><type>bool</type><parameter>strict</parameter><initializer>&false;</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
Détecte l'encodage le plus probable pour la &string;
|
||
<parameter>string</parameter> depuis une liste de candidats.
|
||
</para>
|
||
<para>
|
||
À partir de PHP 8.1, cette fonction utilise des heuristiques pour détecter lequel des encodages de texte valides dans la liste spécifiée
|
||
est le plus susceptible d'être correct et peut ne pas être dans l'ordre des <parameter>encodings</parameter> fournis.
|
||
</para>
|
||
<para>
|
||
La détection automatique du jeu d'encodage prévu n'est jamais totalement
|
||
fiable ; sans information additionnelle, c'est similaire à décoder une
|
||
chaîne chiffrée sans la clé. Il est toujours préférable d'utiliser une
|
||
indication du jeu d'encodage stockée ou transmise avec les données,
|
||
tel que l'en-tête HTTP "Content-Type".
|
||
</para>
|
||
<para>
|
||
Cette fonction est la plus utile avec les encodages multioctets, où
|
||
pas toutes les séquences d'octets forment une chaîne valide. Si la chaîne
|
||
d'entrée contient une telle séquence,
|
||
cet encodage sera rejeté.
|
||
</para>
|
||
|
||
<warning>
|
||
<title>Le résultat n’est pas fiable</title>
|
||
<para>
|
||
Le nom de cette fonction est trompeur, elle effectue une « supposition » plutôt qu’une « détection ».
|
||
</para>
|
||
<para>
|
||
Les suppositions sont loin d’être précises, et par conséquent, cette fonction ne permet pas
|
||
de détecter avec fiabilité l’encodage correct des caractères.
|
||
</para>
|
||
</warning>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>string</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
La <type>string</type> inspectée.
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>encodings</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
Une liste d'encodages de caractères à essayer. Cette liste peut être spécifiée
|
||
comme un &array; de &string;, ou en tant qu'une &string; unique séparée par des virgules.
|
||
</para>
|
||
<para>
|
||
Si <parameter>encodings</parameter> est omis ou &null;,
|
||
le detect_order actuel (défini avec l'option de configuration
|
||
<link linkend="ini.mbstring.detect-order">mbstring.detect_order</link>,
|
||
ou la fonction <function>mb_detect_order</function>) sera utilisé.
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>strict</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
Contrôle le comportement quand <parameter>string</parameter> n'est
|
||
valide dans aucun des <parameter>encodings</parameter> listés.
|
||
Si <parameter>strict</parameter> est défini à &false;, l'encodage qui
|
||
correspond le plus sera retourné ; si <parameter>strict</parameter>
|
||
est défini à &true;, &false; sera retourné.
|
||
</para>
|
||
<para>
|
||
La valeur par défaut de <parameter>strict</parameter> peut être définie
|
||
avec l'option de configuration
|
||
<link linkend="ini.mbstring.strict-detection">mbstring.strict_detection</link>.
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
L'encodage de caractère détecté, ou &false; si la chaîne n'est valide
|
||
dans aucun des encodages listés.
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="changelog">
|
||
&reftitle.changelog;
|
||
<informaltable>
|
||
<tgroup cols="2">
|
||
<thead>
|
||
<row>
|
||
<entry>&Version;</entry>
|
||
<entry>&Description;</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry>8.2.0</entry>
|
||
<entry>
|
||
<function>mb_detect_encoding</function> ne renverra plus
|
||
les encodages non textuels suivants :
|
||
<literal>"Base64"</literal>, <literal>"QPrint"</literal>,
|
||
<literal>"UUencode"</literal>, <literal>"HTML entities"</literal>,
|
||
<literal>"7 bit"</literal> et <literal>"8 bit"</literal>.
|
||
</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</informaltable>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title>Exemple avec <function>mb_detect_encoding</function></title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$str = "\x95\xB6\x8E\x9A\x83\x52\x81\x5B\x83\x68";
|
||
|
||
// Détecte l'encodage avec le detect_order actuel
|
||
var_dump(mb_detect_encoding($str));
|
||
|
||
// "auto" est modifié selon mbstring.language
|
||
var_dump(mb_detect_encoding($str, "auto"));
|
||
|
||
// Spécifie le paramètre "encodings" avec une liste à virgules
|
||
var_dump(mb_detect_encoding($str, "JIS, eucjp-win, sjis-win"));
|
||
|
||
// Utilisation d'un tableau pour spécifier le paramètre "encodings"
|
||
$encodings = [
|
||
"ASCII",
|
||
"JIS",
|
||
"EUC-JP"
|
||
];
|
||
var_dump(mb_detect_encoding($str, $encodings));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
string(5) "ASCII"
|
||
string(5) "ASCII"
|
||
string(8) "SJIS-win"
|
||
string(5) "ASCII"
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>Effet du paramètre <parameter>strict</parameter></title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// 'áéóú' encodées en ISO-8859-1
|
||
$str = "\xE1\xE9\xF3\xFA";
|
||
|
||
// La chaîne n'est pas valide en ASCII ou UTF-8, mais l'UTF-8 est considéré comme une correspondance plus proche
|
||
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8'], false));
|
||
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8'], true));
|
||
|
||
// Si un encodage valide est trouvé, le paramètre strict ne change pas le résultat
|
||
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8', 'ISO-8859-1'], false));
|
||
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8', 'ISO-8859-1'], true));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
string(5) "UTF-8"
|
||
bool(false)
|
||
string(10) "ISO-8859-1"
|
||
string(10) "ISO-8859-1"
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
Dans certains cas, la même séquence d'octets peut former une chaîne valide
|
||
dans différents encodages de caractères, et il est impossible de déterminer
|
||
quelle interprétation était prévue. Un exemple, parmi tant d'autres,
|
||
la séquence d'octets "\xC4\xA2" pourrait être :
|
||
</para>
|
||
<para>
|
||
<simplelist>
|
||
<member>
|
||
"Ä¢" (U+00C4 LATIN CAPITAL LETTER A WITH DIAERESIS suivi d'U+00A2 CENT SIGN)
|
||
encodé en ISO-8859-1, ISO-8859-15, ou Windows-1252
|
||
</member>
|
||
<member>
|
||
"ФЂ" (U+0424 CYRILLIC CAPITAL LETTER EF suivi d'U+0402 CYRILLIC CAPITAL LETTER
|
||
DJE) encodé en ISO-8859-5
|
||
</member>
|
||
<member>
|
||
"Ģ" (U+0122 LATIN CAPITAL LETTER G WITH CEDILLA) encodé en UTF-8
|
||
</member>
|
||
</simplelist>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>Effet de l'ordre quand plusieurs encodages correspondent</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$str = "\xC4\xA2";
|
||
|
||
// La chaîne est valide dans les trois encodages, mais le premier listé n'est pas toujours celui retourné
|
||
var_dump(mb_detect_encoding($str, ['UTF-8']));
|
||
var_dump(mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1', 'ISO-8859-5'])); // as of php8.1 this returns ISO-8859-1 instead of UTF-8
|
||
var_dump(mb_detect_encoding($str, ['ISO-8859-1', 'ISO-8859-5', 'UTF-8']));
|
||
var_dump(mb_detect_encoding($str, ['ISO-8859-5', 'UTF-8', 'ISO-8859-1']));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
string(5) "UTF-8"
|
||
string(10) "ISO-8859-1"
|
||
string(10) "ISO-8859-1"
|
||
string(10) "ISO-8859-5"
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><function>mb_detect_order</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
|
||
-->
|