PHP mb_detect_encoding() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
mbstring

Definition

In PHP, mb_detect_encoding() looks at a string and guesses which character encoding it uses, from a list you give.

Use strict mode so it only accepts an encoding in which every byte is valid.

Syntax

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

PHP
$enc = mb_detect_encoding($text, ["UTF-8", "ISO-8859-1"], true);
CallWhat it does
mb_detect_encoding("café", ["UTF-8", "ISO-8859-1"], true)Returns "UTF-8".
mb_detect_encoding("\xFF", ["UTF-8"], true)Returns false.

The list order matters. PHP returns the first encoding in the list that fits.

Library declarations
PHP
mb_detect_encoding(string $string, array|string|null $encodings = null, bool $strict = false): string|false

Parameters

ParameterDescription
stringRequired. The text to check.
encodingsOptional. A list of encodings to try, as an array or comma-separated string.
strictOptional. If true, a string must be fully valid in an encoding to match. Default is false.

Return Value

Return ValueDescription
string|falseThe name of the detected encoding, or false if none fit.

PHP Examples

Example 1 — Detect UTF-8

Ask which encoding a normal accented word uses.

PHP
<?php
$text = "café";

echo mb_detect_encoding($text, ["UTF-8", "ISO-8859-1"], true), PHP_EOL;

How It Works

  1. Give a list of encodings to try.
  2. PHP tests UTF-8 first. The text is valid UTF-8.
  3. It returns the first match.
  4. Strict mode makes sure the match is exact.

Example 2 — Fall Back to Latin-1

A Latin-1 byte is not valid UTF-8, so the next encoding in the list wins.

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

echo mb_detect_encoding($text, ["UTF-8", "ISO-8859-1"], true), PHP_EOL;

How It Works

  1. The byte \xE9 alone is not valid UTF-8.
  2. PHP skips UTF-8 in strict mode.
  3. Every byte is valid in ISO-8859-1, so it is chosen.
  4. Use the result as the source encoding for a conversion.

Example 3 — Plain ASCII and No Match

Plain text matches ASCII. Broken bytes may match nothing.

PHP
<?php
echo mb_detect_encoding("hello", ["ASCII", "UTF-8"], true), PHP_EOL;

var_dump(mb_detect_encoding("\xFF\xFE", ["UTF-8"], true));

How It Works

  1. Plain English letters fit ASCII, the first in the list.
  2. The bytes \xFF\xFE are not valid UTF-8.
  3. With only UTF-8 in the list, nothing matches.
  4. The result is false. Handle it in your code.

Frequently Asked Questions

mb_detect_encoding() guesses the character encoding of a string from a list of candidates.
mb_detect_encoding(string $string, array|string|null $encodings = null, bool $strict = false): string|false.
Strict mode rejects an encoding unless every byte is valid in it, which avoids many wrong guesses.
No. Detection is a guess. Short text can be valid in several encodings.
Pass it as the source encoding to mb_convert_encoding() to convert the text to UTF-8.

Next: mb_internal_encoding()

mb_internal_encoding() gets or sets the default encoding for mbstring functions.

mb_internal_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