PHP – How to get the substitution character using mb_substitute_character()?


In PHP, we can use the function mb_substitute_character() to get the substitution character. This function specifies the substitution character when the input character encoding is not valid or the character code does not exist in the output character encoding.

Note: The invalid characters may be substituted with no output, string, or int value (Unicode character code value).

Syntax

string mb_substitute_character($char)

Parameters

This function accepts only one parameter, $char.

  • $char− It specifies the Unicode value as an integer or the strings given below:

    • "none"− It will return no output.

    • "long"− It is used for the output character code value. For example, "U+3000, JIS+7E7E"

    • "entity"− it is used to return the output character entity. For example, "&#x200".

Return Values

If the mb_substitute_character is set, then it will return True for success or else it will return False. If it is not set, then it will return the current setting.

Note: PHP 8.0 does not support passing an empty string to substitute_character.

Example

 Live Demo

<?php
   // It will set the Unicode U+3013
   mb_substitute_character(0x3013);

   // Hexadecimal format
   mb_substitute_character("long");

   // It will display current setting
   echo mb_substitute_character();
?>

Output

long

Updated on: 23-Aug-2021

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements