Skip to content

Instantly share code, notes, and snippets.

@w3spi5
Last active March 10, 2024 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w3spi5/942cb38d687b9bebb449eb671d4c177a to your computer and use it in GitHub Desktop.
Save w3spi5/942cb38d687b9bebb449eb671d4c177a to your computer and use it in GitHub Desktop.
Recursively converts data (strings, arrays, objects) to UTF-8 encoding.
<?php
/**
* Related with utf8 problems and based on differents answers from https://stackoverflow.com/a/19366999/3452348
* This function is at the time of writing, for me, the best improved version existing actually.
*
* This function is designed to uniformly encode data as UTF-8. It handles strings by attempting
* to detect the current encoding and converting it to UTF-8. For arrays and objects, it applies
* the conversion recursively to each element or property. The function tries a predefined list
* of common encodings if automatic detection fails. Note that only public properties of objects
* are processed.
*
* @param mixed $d The data to be converted to UTF-8. Can be a string, an array, or an object.
* @return mixed The converted data in UTF-8, maintaining the same type as the input (string,
* array, or object).
*/
public static function utf8ize($d)
{
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = self::utf8ize($v);
}
} elseif (is_object($d)) {
// Recursively convert object properties
$vars = get_object_vars($d);
foreach ($vars as $k => $v) {
$d->$k = self::utf8ize($v);
}
} elseif (is_string($d)) {
// Try a list of common encodings before converting
$encodings = ['UTF-8', 'ISO-8859-1', 'Windows-1251', 'Windows-1252', 'GB2312', 'BIG5'];
foreach ($encodings as $encoding) {
if (mb_check_encoding($d, $encoding)) {
return mb_convert_encoding($d, 'UTF-8', $encoding);
}
}
// Use 'auto' as a last resort
return mb_convert_encoding($d, 'UTF-8', 'auto');
}
return $d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment