This may be a job for the mb_detect_encoding() function.

In my limited experience with it, it's not 100% reliable when used as a generic "encoding sniffer" - It checks for the presence of certain characters and byte values to make an educated guess - but in this narrow case (it'll need to distinguish just between UTF-8 and ISO-8859-1 ) it should work.

<?php
$text = $entity['Entity']['title'];

echo 'Original : ', $text."<br />";
$enc = mb_detect_encoding($text, "UTF-8,ISO-8859-1");

echo 'Detected encoding '.$enc."<br />";

echo 'Fixed result: '.iconv(text)."<br />";

?>

you may get incorrect results for strings that do not contain special characters, but that is not a problem.

Answer from Pekka on Stack Overflow
🌐
PHP
php.net › manual › en › function.utf8-encode.php
PHP: utf8_encode - Manual
<?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"; ?> The above example will output: c3ab cebb e282ac ·
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-utf8_encode-function
PHP utf8_encode() Function - GeeksforGeeks
September 27, 2024 - Example 1: In this example we demonstrates the use of the utf8_encode() function to convert a string (\x63, representing the character "c") from ISO-8859-1 encoding to UTF-8 format and display it.
🌐
OnlinePHP
onlinephp.io › utf8-encode › manual
utf8_encode - OnlinePHP.io Example
This function does not attempt to guess the current encoding of the provided string, it assumes it is encoded as ISO-8859-1 (also known as "Latin 1") and converts to UTF-8.
🌐
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 ...
Top answer
1 of 5
30

This may be a job for the mb_detect_encoding() function.

In my limited experience with it, it's not 100% reliable when used as a generic "encoding sniffer" - It checks for the presence of certain characters and byte values to make an educated guess - but in this narrow case (it'll need to distinguish just between UTF-8 and ISO-8859-1 ) it should work.

<?php
$text = $entity['Entity']['title'];

echo 'Original : ', $text."<br />";
$enc = mb_detect_encoding($text, "UTF-8,ISO-8859-1");

echo 'Detected encoding '.$enc."<br />";

echo 'Fixed result: '.iconv(text)."<br />";

?>

you may get incorrect results for strings that do not contain special characters, but that is not a problem.

2 of 5
9

I made a function that addresses all this issues. It´s called Encoding::toUTF8().

<?php
$text = $entity['Entity']['title'];
echo 'Original : ', $text."<br />";
echo 'Encoding::toUTF8 : ', Encoding::toUTF8($text)."<br />";
?>

Output:

Original : France Télécom
Encoding::toUTF8 : France Télécom

Original : Cond� Nast Publications
Encoding::toUTF8 : Condé Nast Publications

You dont need to know what the encoding of your strings is as long as you know it is either on Latin1 (iso 8859-1), Windows-1252 or UTF8. The string can have a mix of them too.

Encoding::toUTF8() will convert everything to UTF8.

I did it because a service was giving me a feed of data all messed up, mixing UTF8 and Latin1 in the same string.

Usage:

$utf8_string = Encoding::toUTF8($utf8_or_latin1_or_mixed_string);

$latin1_string = Encoding::toLatin1($utf8_or_latin1_or_mixed_string);

Download:

http://dl.dropbox.com/u/186012/PHP/forceUTF8.zip

I've included another function, Encoding::fixUFT8(), wich will fix every UTF8 string that looks garbled.

Usage:

$utf8_string = Encoding::fixUTF8($garbled_utf8_string);

Examples:

echo Encoding::fixUTF8("Fédération Camerounaise de Football");
echo Encoding::fixUTF8("Fédération Camerounaise de Football");
echo Encoding::fixUTF8("FÃÂédÃÂération Camerounaise de Football");
echo Encoding::fixUTF8("Fédération Camerounaise de Football");

will output:

Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football
🌐
W3docs
w3docs.com › learn-php › utf8-encode.html
utf8_encode() | W3Docs
You can use the utf8_encode() function to do this, like this: ... <‌?php // Note: This example assumes the PHP source file is saved in ISO-8859-1. // If your editor saves files as UTF-8 by default, utf8_encode() will double-encode the string.
🌐
GitHub
gist.github.com › oscar-broman › 3653399
UTF8 encode array/object structure in PHP · GitHub
*/ static function utf8_code_deep($input, $b_encode = TRUE, $b_entity_replace = TRUE) { if (is_string($input)) { if($b_encode) { $input = utf8_encode($input); //return Entities to UTF8 characters //important for interfaces to blackbox-pages ...
Find elsewhere
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › utf-8-encoding--php
UTF-8 Encoding : PHP | Encoding Solutions Across Programming Languages
The mb_convert_encoding() function ... $originalString = "Hello, world!"; // Convert to UTF-8 $utf8String = mb_convert_encoding($originalString, 'UTF-8', 'ISO-8859-1'); // Output the UTF-8 encoded string echo $utf8String; // Outputs: ...
🌐
Toptal
toptal.com › php › a-utf-8-primer-for-php-and-mysql
A Guide to UTF-8 Encoding in PHP and MySQL | Toptal®
January 16, 2026 - A useful function for doing this (which I found here) is the following: function utf8_for_xml($string) { return preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string); }
🌐
Université Pierre et Marie Curie
www-ppti.ufr-info-p6.jussieu.fr › doc-online › PHP › php-chunked-xhtml › function.utf8-encode.html
Converts a string from ISO-8859-1 to UTF-8
Exemple #1 Basic example · <?php // Convert the string 'Zoë' from ISO 8859-1 to UTF-8 $iso8859_1_string = "\x5A\x6F\xEB"; $utf8_string = utf8_encode($iso8859_1_string); echo bin2hex($utf8_string), "\n"; ?> L'exemple ci-dessus va afficher : 5a6fc3ab · utf8_decode() - Converts a string from ...
🌐
OnlinePHP
onlinephp.io › utf8-encode
utf8_encode - Online Tool
PHP Functions · String Manipulation · utf8_encode · Execute utf8_encode with this online tool utf8_encode() - Converts a string from ISO-8859-1 to UTF-8 · Utf8 Encode Online Tool · Manual · Code Examples · Utf8 Encode Online Tool · Manual · Code Examples ·
🌐
Delft Stack
delftstack.com › home › howto › php › php convert to utf 8
PHP UTF-8 Conversion | Delft Stack
March 11, 2025 - <?php $string = "Café"; $utf8_string = utf8_encode($string); echo $utf8_string; ?> ... In this example, the utf8_encode() function converts a string that is assumed to be ISO-8859-1 encoded into UTF-8.
🌐
PHP.Watch
php.watch › versions › 8.2 › utf8_encode-utf8_decode-deprecated
`utf8_encode` and `utf8_decode` functions deprecated - PHP 8.2 • PHP.Watch
The German Eszett character (ß, character sequence \xDF), when passed through utf8_encode("ß") results in Ã. Both of the examples above do not emit any warnings or errors although their resulting text is wrong.
🌐
Locutus
locutus.io › php › xml › utf8_encode
PHP's utf8_encode in TypeScript | Locutus
May 16, 2026 - Or with CommonJS: const { utf8_encode } = require('locutus/php/xml/utf8_encode') Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern. These examples are extracted from test cases that automatically verify our functions against their native counterparts.
🌐
GitHub
github.com › neitanod › forceutf8
GitHub - neitanod/forceutf8: PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings. · GitHub
PHP Class Encoding featuring popular ... that fixes mixed encoded strings. If you apply the PHP function utf8_encode() to an already-UTF8 string it will return a garbled UTF8 string....
Starred by 1.6K users
Forked by 358 users
Languages   PHP