<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.mb-scrub.php',
    1 => 'mb_scrub',
    2 => 'Replace ill-formed byte sequences with the substitute character',
  ),
  'up' => 
  array (
    0 => 'ref.mbstring.php',
    1 => 'Multibyte String Functions',
  ),
  'prev' => 
  array (
    0 => 'function.mb-rtrim.php',
    1 => 'mb_rtrim',
  ),
  'next' => 
  array (
    0 => 'function.mb-send-mail.php',
    1 => 'mb_send_mail',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mbstring/functions/mb-scrub.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.mb-scrub" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mb_scrub</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.2.0, PHP 8)</p><p class="refpurpose"><span class="refname">mb_scrub</span> &mdash; <span class="dc-title">Replace ill-formed byte sequences with the substitute character</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.mb-scrub-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">function</span> <span class="methodname"><strong>mb_scrub</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.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$encoding</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   Perform a character set conversion from the specified encoding, or the default encoding if no
   encoding was specified, to the same encoding. This has the effect of replacing any invalid
   byte sequences with the substitute character.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.mb-scrub-parameters">
  <h3 class="title">Parameters</h3>
  <dl>
   
    <dt><code class="parameter">string</code></dt>
    <dd>
     <p class="para">
      The input string.
     </p>
    </dd>
   
   
    <dt><code class="parameter">encoding</code></dt>
    <dd>
     <p class="para">
      The encoding used to interpret <code class="parameter">string</code>.
      If it is omitted or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, the
      <a href="mbstring.configuration.php#ini.mbstring.internal-encoding" class="link">mbstring.internal_encoding setting</a>
      will be used if set, otherwise the <a href="ini.core.php#ini.default-charset" class="link">default_charset setting</a>
      will be used.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mb-scrub-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The <span class="type"><a href="language.types.string.php" class="type string">string</a></span> result with invalid byte sequences replaced.
  </p>
 </div>


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

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.0.0</td>
 <td>
  <code class="parameter">encoding</code> is nullable now.
 </td>
</tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.mb-scrub-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 Byte-level replacement performed by <span class="function"><strong>mb_scrub()</strong></span></strong></p>
   <div class="example-contents"><p>
    <span class="function"><a href="function.bin2hex.php" class="function">bin2hex()</a></span> is used here because terminals, browsers and
    fonts may render an ill-formed byte sequence with a replacement character
    of their own, which hides what the string actually contains.
   </p></div>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #FF8000">// The byte 0xFF cannot appear in a valid UTF-8 string.
</span><span style="color: #0000BB">$input </span><span style="color: #007700">= </span><span style="color: #DD0000">"A\xFFB"</span><span style="color: #007700">;
echo </span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;

</span><span style="color: #FF8000">// The default substitute character is "?" (0x3F).
</span><span style="color: #007700">echo </span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_scrub</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">)), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;

</span><span style="color: #FF8000">// U+FFFD REPLACEMENT CHARACTER is encoded as EF BF BD in UTF-8.
</span><span style="color: #0000BB">mb_substitute_character</span><span style="color: #007700">(</span><span style="color: #0000BB">0xFFFD</span><span style="color: #007700">);
echo </span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">mb_scrub</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">)), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;

</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">41ff42
413f42
41efbfbd42</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-2">
   <p><strong>Example #2 Using <span class="function"><strong>mb_scrub()</strong></span> before UTF-8 aware processing</strong></p>
   <div class="example-contents"><p>
    PCRE patterns using the <code class="literal">u</code> modifier reject subjects that
    are not well-formed UTF-8. Scrubbing the input first makes it acceptable.
   </p></div>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

$input </span><span style="color: #007700">= </span><span style="color: #DD0000">"A\xFFB"</span><span style="color: #007700">;

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">'/./us'</span><span style="color: #007700">, </span><span style="color: #0000BB">$input</span><span style="color: #007700">));
echo </span><span style="color: #0000BB">preg_last_error_msg</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;

</span><span style="color: #0000BB">$clean </span><span style="color: #007700">= </span><span style="color: #0000BB">mb_scrub</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">);

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">'/./us'</span><span style="color: #007700">, </span><span style="color: #0000BB">$clean</span><span style="color: #007700">));

</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">bool(false)
Malformed UTF-8 characters, possibly incorrectly encoded
int(3)</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mb-scrub-seealso">
  <h3 class="title">See Also</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.mb-substitute-character.php" class="function" rel="rdfs-seeAlso">mb_substitute_character()</a> - Set/Get substitution character</span></li>
   <li><span class="function"><a href="function.mb-check-encoding.php" class="function" rel="rdfs-seeAlso">mb_check_encoding()</a> - Check if strings are valid for the specified encoding</span></li>
   <li><span class="function"><a href="function.mb-convert-encoding.php" class="function" rel="rdfs-seeAlso">mb_convert_encoding()</a> - Convert a string from one character encoding to another</span></li>
  </ul>
 </div>


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