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 ·
🌐
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
Top answer
1 of 4
11

If you read the manual entry for utf8_encode, it converts an ISO-8859-1 encoded string to UTF-8. The function name is a horrible misnomer, as it suggests some sort of automagic encoding that is necessary. That is not the case. If your source code is saved as UTF-8 and you assign "あ" to $string, then $string holds the character "あ" encoded in UTF-8. No further action is necessary. In fact, trying to convert the UTF-8 string (incorrectly) from ISO-8859-1 to UTF-8 will garble it.

To elaborate a little more, your source code is read as a byte sequence. PHP interprets the stuff that is important to it (all the keywords and operators and so on) in ASCII. UTF-8 is backwards compatible to ASCII. That means, all the "normal" ASCII characters are represented using the same byte in both ASCII and UTF-8. So a " is interpreted as a " by PHP regardless of whether it's supposed to be saved in ASCII or UTF-8. Anything between quotes, PHP simply takes as the literal bit sequence. So PHP sees your "あ" as "11100011 10000001 10000010". It doesn't care what exactly is between the quotes, it'll just use it as-is.

2 of 4
3

PHP does not care about string encoding generally, strings are binary data within PHP. So you must know the encoding of data inside the string if you need encoding. The question is: does encoding matter in your case?

If you set a string variables content to something like you did:

$string="ぁ";

It will not contain UTF-8. Instead it contains a binary sequence that is not a valid UTF-8 character. That's why the browser or editor displays a questionmark or similar. So before you go on, you already see that something might not be as intended. (Turned out it was a missing font on my end)

This also shows that your file in the editor is supporting UTF-8 or some other flavor of unicode encoding. Just keep the following in mind: One file - one encoding. If you store the string inside the file, it's in the encoding of that file. Check your editor in which encoding you save the file. Then you know the encoding of the string.

Let's just assume it is some valid UTF-8 like so (support for my font):

$string="ä";

You can then do a binary comparison of the string later on:

if ( 'ä' === $string )
  # do your stuff

Because it's in the same file and PHP strings are binary data, this works with every encoding. So normally you don't need to re-encode (change the encoding) the data if you use functions that are binary safe - which means that the encoding of the data is not changed.

For regular expressions encoding does play a role. That's why there is the u modifier to signal you want to make the expression work on and with unicode encoded data. However, if the data is already unicode encoded, you don't need to change it into unicode before you use preg_match. However with your code example, regular expressions are not necessary at all and a simple string comparison does the job.

Summary:

$string="ä";
if ( 'ä' === $string )
  # do your stuff