The mb_check_encoding() function is an inbuilt function in PHP that is used to check whether a given string is valid for specified encoding or not.
Syntax:
bool mb_check_encoding( array|string|null $value = null, string $encoding = null )
Parameters: This function accepts two parameters that are described below:
- $value: This parameter accepts the byte stream or array to check. If this value is null then it checks for all input from the beginning to the request.
- $encoding: This parameter accepts the expected encoding.
Return Value: This function returns “true” on success and “false” on Failure.
Note:
- The value of $value and $encoding parameters are null in version 8.0.0.
- This function now accepts an array of values and formally it accepts only string values.
Example 1: The following code demonstrates the PHP mb_check_encoding() method.
PHP
<?php // Declare a variable and assign // string value $str = "Welcome to neveropen" ; // Check string for specified encoding $bool = mb_check_encoding( $str , "ASCII" ); // Returns true means string is // valid for specified encoding var_dump( $bool ); ?> |
bool(true)
Example 2: The following code demonstrates another example for the PHP mb_check_encoding() method.
PHP
<?php // Declare a variable and assign // string value $str = "Welcome to neveropen" ; // Check string for base64 encoding $bool = mb_check_encoding( $str , "base64" ); // Returns true means string is // valid for specified encoding var_dump( $bool ); ?> |
bool(false)
Reference: https://www.php.net/manual/en/function.mb-check-encoding.php