Current Location: Home> Latest Articles> Detailed explanation of function parameters and common errors

Detailed explanation of function parameters and common errors

gitbox 2025-06-03

1. Introduction to mb_substitute_character function

The function of mb_substitute_character is to set the multi-byte string function ( mb_convert_encoding , etc.) to replace characters that cannot be converted. Its function prototype is as follows:

 mb_substitute_character([ mixed $substchar = null ]): mixed
  • Parameter $substchar : Optional, specifying the type of the substitute character.

  • Return value : If a parameter is passed, the previous substitute character setting is returned; if the parameter is not passed, the current substitute character setting is returned.


2. How to write parameters?

The $substchar parameter can accept values ​​of the following types:

  1. Integer value (0-255)
    ASCII code representing single-byte characters. For example:

 mb_substitute_character(63); // Set the alternative character toASCIIQuestion mark '?'
  1. String <br> A string can be passed in to represent specific alternative characters. For example:

 mb_substitute_character('?'); // use“Alternative characters”symbol(U+FFFD)
  1. Predefined constants
    PHP has several commonly used alternative character constants built into:

    • MB_SUBSTITUTE_NONE : Not replaced, if the wrong characters are encountered, they will be discarded directly.

    • MB_SUBSTITUTE_CHARACTER : Use alternative characters (usually "?" or "?").

    • MB_SUBSTITUTE_ENTITY : Use HTML entity instead.

    • MB_SUBSTITUTE_STRING : Customized substitute string (need to be additionally matched with mb_substitute_character settings).

For example:

 mb_substitute_character(MB_SUBSTITUTE_CHARACTER);

3. Common errors and misunderstandings

  1. Passing in illegal numbers or strings <br> Many beginners will randomly pass a number or string, resulting in invalid substitute character settings or unexpected results. mb_substitute_character requires that the number must be between 0-255 and the string must be a single character or a predefined constant.

  2. Ignore the return value <br> This function returns the previous alternative character settings, and ignoring the return value may make it difficult to track the current alternative character status in complex programs.

  3. Misuse of constant names <br> Sometimes it will be misspelled or used incorrectly. For example, writing it as MB_SUBSTITUTE_CHAR instead of MB_SUBSTITUTE_CHARACTER results in an error.

  4. Confusing alternative characters with encoding settings
    mb_substitute_character only sets substitute characters and has no direct relationship with mb_internal_encoding or mb_detect_encoding , but is easily misunderstood.


4. Sample code

 <?php
// Set the alternative character to问号 '?'
mb_substitute_character(63);

$str = "こんにちはworld"; // "Hello world"Japanese
// 故意use错误编码转换
$converted = mb_convert_encoding($str, "ASCII", "UTF-8");

echo $converted; // Characters that cannot be converted are replaced by '?'
?>

5. Official document reference

For more details, please check the official PHP documentation:

<code> https://gitbox.net/manual/zh/function.mb-substitute-character.php </code>