Open In App

PHP mb_check_encoding() Function

Last Updated : 31 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 GeeksforGeeks";
  
    // Check string for specified encoding
    $bool = mb_check_encoding($str, "ASCII");
  
    // Returns true means string is
    // valid for specified encoding
    var_dump($bool);
  
?>


Output

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 GeeksforGeeks";
  
    // Check string for base64 encoding
    $bool = mb_check_encoding($str, "base64");
  
    // Returns true means string is
    // valid for specified encoding
    var_dump($bool);
  
?>


Output

bool(false)

Reference: https://www.php.net/manual/en/function.mb-check-encoding.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads