What you're asking for is extremely hard. If possible, getting the user to specify the encoding is the best. Preventing an attack shouldn't be much easier or harder that way.

However, you could try doing this:

iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);

Setting it to strict might help you get a better result.

Answer from Jeff Day on Stack Overflow
🌐
PHP
php.net › manual › en › function.utf8-encode.php
PHP: utf8_encode - Manual
Similar functionality can be achieved with mb_convert_encoding(), which supports ISO-8859-1 and many other character encodings. <?php $iso8859_1_string = "\xEB"; // 'ë' (e with diaeresis) in ISO-8859-1 $utf8_string = mb_convert_encoding($iso8859_1_string, 'UTF-8', 'ISO-8859-1'); echo bin2hex($utf8_string), "\n"; $iso8859_7_string = "\xEB"; // the same string in ISO-8859-7 represents 'λ' (Greek lower-case lambda) $utf8_string = mb_convert_encoding($iso8859_7_string, 'UTF-8', 'ISO-8859-7'); echo bin2hex($utf8_string), "\n"; $windows_1252_string = "\x80"; // '€' (Euro sign) in Windows-1252, but not in ISO-8859-1 $utf8_string = mb_convert_encoding($windows_1252_string, 'UTF-8', 'Windows-1252'); echo bin2hex($utf8_string), "\n"; ?>
Top answer
1 of 14
300

What you're asking for is extremely hard. If possible, getting the user to specify the encoding is the best. Preventing an attack shouldn't be much easier or harder that way.

However, you could try doing this:

iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);

Setting it to strict might help you get a better result.

2 of 14
33

In motherland Russia we have four popular encodings, so your question is in great demand here.

Only by character codes of symbols you can not detect the encoding, because code pages intersect. Some codepages in different languages have even full intersection. So, we need another approach.

The only way to work with unknown encodings is working with probabilities. So, we do not want to answer the question "what is encoding of this text?", we are trying to understand "what is most likely encoding of this text?".

One guy here in a popular Russian tech blog invented this approach:

Build the probability range of character codes in every encoding you want to support. You can build it using some big texts in your language (e.g., some fiction, use Shakespeare for English and Tolstoy for Russian, LOL). You will get something like this:

    encoding_1:
    190 => 0.095249209893009,
    222 => 0.095249209893009,
    ...
    encoding_2:
    239 => 0.095249209893009,
    207 => 0.095249209893009,
    ...
    encoding_N:
    charcode => probabilty

Next, you take text in an unknown encoding and for every encoding in your "probability dictionary" you search for the frequency of every symbol in the unknown-encoded text. Sum the probabilities of symbols. Encoding with the bigger rating is likely the winner. There are better results for bigger texts.

Btw, mb_detect_encoding certainly does not work. Yes, at all. Please, take a look of the mb_detect_encoding source code in "ext/mbstring/libmbfl/mbfl/mbfl_ident.c".

People also ask

How to insert Unicode characters in MySQL using PHP?
In order to insert Unicode characters in MySQL, you need to create a table with Unicode support, select the appropriate encoding/collation settings, and specify the charset in the MySQL connection. Then, you can proceed and employ PHP code to insert Unicode as you please.
🌐
toptal.com
toptal.com › developers › php › a-utf-8-primer-for-php-and-mysql
A Guide to UTF-8 Encoding in PHP and MySQL | Toptal®
What is UTF-8 character set?
Defined by the Unicode standard, UTF-8 is an 8-bit character encoding capable of storing ay Unicode character. It is backwards compatible with ASCII.
🌐
toptal.com
toptal.com › developers › php › a-utf-8-primer-for-php-and-mysql
A Guide to UTF-8 Encoding in PHP and MySQL | Toptal®
What does UTF-8 stand for?
UTF is short for Unicode Transformation Format, while the “8” suffix denotes the use of 8-bit blocks to represent characters.
🌐
toptal.com
toptal.com › developers › php › a-utf-8-primer-for-php-and-mysql
A Guide to UTF-8 Encoding in PHP and MySQL | Toptal®
🌐
W3Schools
w3schools.com › Php › func_xml_utf8_encode.asp
PHP utf8_encode() Function
Encode an ISO-8859-1 string to UTF-8: <?php $text = "\xE0"; echo utf8_encode($text); ?> Try it Yourself » · The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Note: This function was deprecated in PHP 8.2.0. Unicode is a universal ...
🌐
PHP.Watch
php.watch › versions › 8.2 › utf8_encode-utf8_decode-deprecated
`utf8_encode` and `utf8_decode` functions deprecated - PHP 8.2 • PHP.Watch
Alternatively, it is possible to directly convert code-points to UTF-8 string as well using user-land PHP albeit with a small performance penalty. When the use case of utf8_encode is to automatically detect the character encoding and convert it to UTF-8, even though the function did not detect character encodings in the first place, the replacement would be detecting the character encoding first, and then converting it to UTF-8.
🌐
Reddit
reddit.com › r/php › why not php default string encoding utf-8 ?
r/PHP on Reddit: Why not PHP default string encoding UTF-8 ?
June 6, 2024 -

Many languages use UTF-8 for their strings. As languages progress, UTF-8 has become the default (e.g. Java)

PHP has mb_str* , str functions. Why are they separate? can't UTF-8 be the default, and these functions be combined?

just curious about this.

Find elsewhere
🌐
Honeybadger
honeybadger.io › blog › php-character-encoding-unicode-utf8-ascii
Character Encoding for PHP Developers: Unicode, UTF-8 and ASCII - Honeybadger Developer Blog
May 17, 2021 - To achieve this, we can use mb_check_encoding, and it'll return true or false. For example, to check that a string is in UTF-8: ... Since it's so common to render some HTML code for a website from PHP, here's how we can make sure that we set ...
🌐
Toptal
toptal.com › developers › php › a-utf-8-primer-for-php-and-mysql
A Guide to UTF-8 Encoding in PHP and MySQL | Toptal®
January 16, 2026 - In every PHP output header, specify UTF-8 as the encoding: ... Since not all UTF-8 characters are accepted in an XML document, you’ll need to strip any such characters out from any XML that you generate.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-utf8_encode-function
PHP utf8_encode() Function - GeeksforGeeks
September 27, 2024 - The utf8_encode() function in PHP converts ISO-8859-1 (Latin-1) encoded strings to UTF-8 encoding.
🌐
GitHub
gist.github.com › oscar-broman › 3653399
UTF8 encode array/object structure in PHP · GitHub
} } else { //Replace NON-ISO Characters with their Entities to stop setting them to '?'-Characters. if($b_entity_replace) { $input = preg_replace("/([\304-\337])([\200-\277])/e", "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'", $input); } $input = utf8_decode($input); } } elseif (is_array($input)) { foreach ($input as &$value) { $value = self::utf8_code_deep($value, $b_encode, $b_entity_replace); } } elseif (is_object($input)) { $vars = array_keys(get_object_vars($input)); if(get_class($input) == 'SimpleXMLElement') { //DOES NOT WORK! return ''; } foreach ($vars as $var) { $input->$var = self::utf8_code_deep($input->$var, $b_encode, $b_entity_replace); } } return $input; } } ?> ... `<?php final class Tools { static function utf8_code_deep($input, $b_encode = TRUE, $b_entity_replace = TRUE) { if (is_string($input)) { if($b_encode) { $input = utf8_encode($input);
🌐
DopeThemes
dopethemes.com › home › utf8_encode/utf8_decode replacement: modern php encoding alternatives
utf8_encode/utf8_decode Replacement: Modern PHP Encoding Alternatives - DopeThemes
August 29, 2024 - The old utf8_encode( $iso_string ) is now mb_convert_encoding( $iso_string, 'UTF-8', 'ISO-8859-1' ). Same result, but now the source encoding is stated, not assumed. ... <?php $utf8_string = 'This is a UTF-8 string with characters: é, ç, ü'; ...
🌐
Drupal
drupal.org › project › feeds › issues › 3426334
PHP 8.2 Deprecated function: utf8_encode() [#3426334] | Drupal.org
March 25, 2024 - So the desired encoding of the result, in this case 'UTF-8' is the second parameter. The third parameter is the current encoding used to interpret string, in this case ' ISO-8859-1'. By the way: I see that the tests report much more PHP 8.2 deprecations, but these could be handled in an other issue.
🌐
Drupal
drupal.org › project › imce › issues › 3410945
PHP 8.2: utf8_encode is deprecated [#3410945] | Drupal.org
February 23, 2025 - Problem/Motivation PHP 8.2 notes that utf8_encode() and utf8_decode() are deprecated because they can introduce obscure bugs. More info here. Steps to reproduce Use of the functions are found in branch 7.x-2.x and below. 8.x and 3.x don't use these functions.
🌐
Lindevs
lindevs.com › functions-utf8_encode-and-utf8_decode-are-deprecated-in-php-8-2
Functions utf8_encode and utf8_decode are Deprecated in PHP 8.2 | Lindevs
December 12, 2022 - <?php $utf8 = utf8_encode("\xa5\xa7\xb5"); // ISO-8859-1 -> UTF-8 $iso88591 = utf8_decode($utf8); // UTF-8 -> ISO-8859-1
🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › php › func_xml_utf8_encode.asp.html
PHP utf8_encode() Function
PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones ... The utf8_encode() function encodes an ISO-8859-1 string to UTF-8.
🌐
mixable Blog
mixable.blog › home › php: function utf8_decode() and utf8_encode() have been deprecated
PHP: utf8_decode() and utf8_encode() have been deprecated | mixable Blog
May 29, 2024 - The utf8_encode() and utf8_decode() functions in PHP are used for encoding and decoding strings between ISO-8859-1 (Latin-1) and UTF-8.
🌐
OnlinePHP
onlinephp.io › utf8-encode › manual
utf8_encode - OnlinePHP.io Example
This function converts the string string from the ISO-8859-1 encoding to UTF-8.
🌐
PHP
wiki.php.net › rfc › remove_utf8_decode_and_utf8_encode
PHP: rfc:remove_utf8_decode_and_utf8_encode
April 4, 2022 - The Latin 1 encoding is commonly confused with other encodings, particularly Windows Code Page 1252. The lack of error messages means that incorrect use is not easy to spot. This RFC takes the view that their inclusion under the current name does more harm than good, and that removing them will encourage users to find more appropriate functions for their use cases. In PHP 8.2, all uses of utf8_encode and utf8_decode will raise a standard E_DEPRECATED diagnostic (“Function utf8_encode() is deprecated” / “Function utf8_decode() is deprecated”).
🌐
Derick Rethans
derickrethans.nl › phpinternalsnews-98.html
PHP Internals News: Episode 98: Deprecating utf8_encode and utf8_decode — Derick Rethans
March 3, 2022 - PHP has these two functions, utf8_encode and utf8_decode, which, in themselves, they're not broken. They do what they are designed to do. But they are very frequently misunderstood. Mostly because of their name. And because Character Encodings in general, are not very well understood.
🌐
Locutus
locutus.io › php › xml › utf8_encode
PHP's utf8_encode in TypeScript | Locutus
May 16, 2026 - Install via yarn add locutus and import: import { utf8_encode } from 'locutus/php/xml/utf8_encode'.