The mb_substitute_character Function

The mb_substitute_character function allows you to set the character that will be used to replace invalid byte sequences in multibyte strings. As a parameter, you can pass an integer (character code) or the string "none" to disable substitution, or "long" to output the numeric representation of the invalid character.

Syntax

phpmb_substitute_character([mixed $substitute_char]): mixed

Example

Let's set the substitution character as a question mark:

php<?php
  mb_substitute_character('?');
  echo mb_convert_encoding("\x80", "UTF-8", "ISO-8859-1");
?>

Code execution result:

php'?'

Example

Let's get the current substitution character:

php<?php
  $res = mb_substitute_character();
  var_dump($res);
?>

Code execution result (character code '?'):

php63

Example

Let's disable the substitution of invalid characters:

php<?php
  mb_substitute_character("none");
  echo mb_convert_encoding("\x80", "UTF-8", "ISO-8859-1");
?>

Code execution result:

php''