PHP mb_check_encoding() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
mbstring

Definition

In PHP, mb_check_encoding() checks whether every byte of a string is valid in a given character encoding.

It is the quick way to test user input before you store or process it.

Syntax

Call mb_check_encoding() with the values you need. Start with this line.

PHP
$valid = mb_check_encoding($text, "UTF-8");
CallWhat it does
mb_check_encoding("café", "UTF-8")Returns true.
mb_check_encoding("caf\xE9", "UTF-8")Returns false.

If you leave out the encoding, the internal encoding is used. It is UTF-8 on most servers.

Library declarations
PHP
mb_check_encoding(array|string|null $value = null, ?string $encoding = null): bool

Parameters

ParameterDescription
valueRequired in practice. The string (or array of strings) to check.
encodingOptional. The encoding to check against. Default is the internal encoding.

Return Value

Return ValueDescription
booltrue if the value is valid in that encoding, false if not.

PHP Examples

Example 1 — Check a UTF-8 String

A normal accented word is valid UTF-8.

PHP
<?php
var_dump(mb_check_encoding("café", "UTF-8"));

How It Works

  1. Pass the text. It is saved as UTF-8.
  2. Pass the encoding name. Here it is UTF-8.
  3. Get true. Every byte is valid.
  4. Use var_dump() to see the type.

Example 2 — Spot an Invalid String

A single Latin-1 byte is not valid UTF-8.

PHP
<?php
$text = "caf\xE9";

var_dump(mb_check_encoding($text, "UTF-8"));
var_dump(mb_check_encoding($text, "ISO-8859-1"));

How It Works

  1. Use a Latin-1 byte. \xE9 is é in ISO-8859-1.
  2. Check it as UTF-8. The byte stands alone, so it is invalid.
  3. Check it as ISO-8859-1. Every byte is valid there.
  4. Use the result to choose the right conversion.

Example 3 — Validate Form Input

Reject input that is not valid UTF-8.

PHP
<?php
$input = "ok\xC3";

if (mb_check_encoding($input, "UTF-8")) {
    echo "valid", PHP_EOL;
} else {
    echo "invalid", PHP_EOL;
}

How It Works

  1. The byte \xC3 starts a two-byte letter but has no second byte.
  2. Check it as UTF-8.
  3. Branch on the result. Here you reach else.
  4. Reject or convert the input before you use it.

Frequently Asked Questions

mb_check_encoding() tells you whether a string is valid in a given character encoding.
mb_check_encoding(array|string|null $value = null, ?string $encoding = null): bool.
Yes. Since PHP 7.2 you can pass an array and every key and value is checked.
PHP checks against the internal encoding, which is usually UTF-8.
Convert it from the right source encoding with mb_convert_encoding(), for example from ISO-8859-1 to UTF-8.

Next: mb_convert_encoding()

mb_convert_encoding() converts a string from one character encoding to another.

mb_convert_encoding() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful