<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.mbstring.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.mb-detect-encoding.php',
    1 => 'mb_detect_encoding',
    2 => 'D&eacute;tecte un encodage',
  ),
  'up' => 
  array (
    0 => 'ref.mbstring.php',
    1 => 'Fonctions sur les cha&icirc;nes de caract&egrave;res multioctets',
  ),
  'prev' => 
  array (
    0 => 'function.mb-decode-numericentity.php',
    1 => 'mb_decode_numericentity',
  ),
  'next' => 
  array (
    0 => 'function.mb-detect-order.php',
    1 => 'mb_detect_order',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/mbstring/functions/mb-detect-encoding.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.mb-detect-encoding" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mb_detect_encoding</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">mb_detect_encoding</span> &mdash; <span class="dc-title">Détecte un encodage</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.mb-detect-encoding-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mb_detect_encoding</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$encodings</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$strict</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   Détecte l&#039;encodage le plus probable pour la <a href="language.types.string.php" class="link">chaîne de caractères</a>
   <code class="parameter">string</code> depuis une liste de candidats.
  </p>
  <p class="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&#039;être correct et peut ne pas être dans l&#039;ordre des <code class="parameter">encodings</code> fournis.
  </p>
  <p class="para">
   La détection automatique du jeu d&#039;encodage prévu n&#039;est jamais totalement
   fiable ; sans information additionnelle, c&#039;est similaire à décoder une
   chaîne chiffrée sans la clé. Il est toujours préférable d&#039;utiliser une
   indication du jeu d&#039;encodage stockée ou transmise avec les données,
   tel que l&#039;en-tête HTTP &quot;Content-Type&quot;.
  </p>
  <p class="para">
   Cette fonction est la plus utile avec les encodages multioctets, où
   pas toutes les séquences d&#039;octets forment une chaîne valide. Si la chaîne
   d&#039;entrée contient une telle séquence,
   cet encodage sera rejeté.
  </p>

  <div class="warning"><strong class="warning">Avertissement</strong>
   <h1 class="title">Le résultat n’est pas fiable</h1>
   <p class="para">
    Le nom de cette fonction est trompeur, elle effectue une « supposition » plutôt qu’une « détection ».
   </p>
   <p class="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.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mb-detect-encoding-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string</code></dt>
     <dd>
      <p class="para">
       La <span class="type"><a href="language.types.string.php" class="type string">string</a></span> inspectée.
      </p>
     </dd>
    
    
     <dt><code class="parameter">encodings</code></dt>
     <dd>
      <p class="para">
       Une liste d&#039;encodages de caractères à essayer. Cette liste peut être spécifiée
       comme un <a href="language.types.array.php" class="link">tableau</a> de <a href="language.types.string.php" class="link">chaîne de caractères</a>, ou en tant qu&#039;une <a href="language.types.string.php" class="link">chaîne de caractères</a> unique séparée par des virgules.
      </p>
      <p class="para">
       Si <code class="parameter">encodings</code> est omis ou <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>,
       le detect_order actuel (défini avec l&#039;option de configuration
       <a href="mbstring.configuration.php#ini.mbstring.detect-order" class="link">mbstring.detect_order</a>,
       ou la fonction <span class="function"><a href="function.mb-detect-order.php" class="function">mb_detect_order()</a></span>) sera utilisé.
      </p>
     </dd>
    
    
     <dt><code class="parameter">strict</code></dt>
     <dd>
      <p class="para">
       Contrôle le comportement quand <code class="parameter">string</code> n&#039;est
       valide dans aucun des <code class="parameter">encodings</code> listés.
       Si <code class="parameter">strict</code> est défini à <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, l&#039;encodage qui
       correspond le plus sera retourné ; si <code class="parameter">strict</code>
       est défini à <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> sera retourné.
      </p>
      <p class="para">
       La valeur par défaut de <code class="parameter">strict</code> peut être définie
       avec l&#039;option de configuration
       <a href="mbstring.configuration.php#ini.mbstring.strict-detection" class="link">mbstring.strict_detection</a>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mb-detect-encoding-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   L&#039;encodage de caractère détecté, ou <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> si la chaîne n&#039;est valide
   dans aucun des encodages listés.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.mb-detect-encoding-changelog">
  <h3 class="title">Historique</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.2.0</td>
      <td>
       <span class="function"><strong>mb_detect_encoding()</strong></span> ne renverra plus
       les encodages non textuels suivants :
       <code class="literal">&quot;Base64&quot;</code>, <code class="literal">&quot;QPrint&quot;</code>,
       <code class="literal">&quot;UUencode&quot;</code>, <code class="literal">&quot;HTML entities&quot;</code>,
       <code class="literal">&quot;7 bit&quot;</code> et <code class="literal">&quot;8 bit&quot;</code>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.mb-detect-encoding-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Exemple avec <span class="function"><strong>mb_detect_encoding()</strong></span></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"\x95\xB6\x8E\x9A\x83\x52\x81\x5B\x83\x68"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Détecte l'encodage avec le detect_order actuel<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// "auto" est modifié selon mbstring.language<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #DD0000">"auto"</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Spécifie le paramètre "encodings" avec une liste à virgules<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #DD0000">"JIS, eucjp-win, sjis-win"</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Utilisation d'un tableau pour spécifier le paramètre "encodings"<br /></span><span style="color: #0000BB">$encodings </span><span style="color: #007700">= [<br />  </span><span style="color: #DD0000">"ASCII"</span><span style="color: #007700">,<br />  </span><span style="color: #DD0000">"JIS"</span><span style="color: #007700">,<br />  </span><span style="color: #DD0000">"EUC-JP"<br /></span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">$encodings</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">string(5) &quot;ASCII&quot;
string(5) &quot;ASCII&quot;
string(8) &quot;SJIS-win&quot;
string(5) &quot;ASCII&quot;</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Effet du paramètre <code class="parameter">strict</code></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// 'áéóú' encodées en ISO-8859-1<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"\xE1\xE9\xF3\xFA"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// La chaîne n'est pas valide en ASCII ou UTF-8, mais l'UTF-8 est considéré comme une correspondance plus proche<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, [</span><span style="color: #DD0000">'ASCII'</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">], </span><span style="color: #0000BB">false</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, [</span><span style="color: #DD0000">'ASCII'</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">], </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Si un encodage valide est trouvé, le paramètre strict ne change pas le résultat<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, [</span><span style="color: #DD0000">'ASCII'</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ISO-8859-1'</span><span style="color: #007700">], </span><span style="color: #0000BB">false</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, [</span><span style="color: #DD0000">'ASCII'</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ISO-8859-1'</span><span style="color: #007700">], </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">string(5) &quot;UTF-8&quot;
bool(false)
string(10) &quot;ISO-8859-1&quot;
string(10) &quot;ISO-8859-1&quot;</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   Dans certains cas, la même séquence d&#039;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&#039;autres,
   la séquence d&#039;octets &quot;\xC4\xA2&quot; pourrait être :
  </p>
  <p class="para">
   <ul class="simplelist">
    <li>
     "Ä¢" (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
    </li>
    <li>
     "ФЂ" (U+0424 CYRILLIC CAPITAL LETTER EF suivi d'U+0402 CYRILLIC CAPITAL LETTER
     DJE) encodé en ISO-8859-5
    </li>
    <li>
     "Ģ" (U+0122 LATIN CAPITAL LETTER G WITH CEDILLA) encodé en UTF-8
    </li>
   </ul>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Effet de l&#039;ordre quand plusieurs encodages correspondent</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"\xC4\xA2"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// La chaîne est valide dans les trois encodages, mais le premier listé n'est pas toujours celui retourné<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, [</span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, [</span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ISO-8859-1'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ISO-8859-5'</span><span style="color: #007700">])); </span><span style="color: #FF8000">// as of php8.1 this returns ISO-8859-1 instead of UTF-8<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, [</span><span style="color: #DD0000">'ISO-8859-1'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ISO-8859-5'</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, [</span><span style="color: #DD0000">'ISO-8859-5'</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ISO-8859-1'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">string(5) &quot;UTF-8&quot;
string(10) &quot;ISO-8859-1&quot;
string(10) &quot;ISO-8859-1&quot;
string(10) &quot;ISO-8859-5&quot;</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mb-detect-encoding-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.mb-detect-order.php" class="function" rel="rdfs-seeAlso">mb_detect_order()</a> - Lit/modifie l'ordre de d&eacute;tection des encodages</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>