Last active
June 2, 2016 14:52
-
-
Save Langmans/c1c16369ea96831400bd to your computer and use it in GitHub Desktop.
Anything to UTF-8. Support for arrays and object to string conversion as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converts anything to UTF-8. | |
* | |
* @param $string string|object|array | |
* @param $stringify_objects bool|true | |
* @param $throw_exceptions bool|true | |
* | |
* @throws InvalidArgumentException | |
* @throws RuntimeException | |
* | |
* @return array|string | |
*/ | |
function to_utf8($string, $stringify_objects = true, $throw_exceptions = true) | |
{ | |
if ($stringify_objects && is_object($string) && method_exists($string, '__toString')) { | |
$string = (string)$string; | |
} | |
if (is_array($string)) { | |
$out = array(); | |
foreach ($string as $i => $v) { | |
$out[to_utf8($i, $stringify_objects, $throw_exceptions)] = to_utf8($v, $stringify_objects, $throw_exceptions); | |
} | |
return $out; | |
} elseif (is_string($string)) { | |
$detected = mb_detect_encoding($string, | |
'UTF-8, ASCII, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-13, ISO-8859-14, ISO-8859-15, ISO-8859-16, Windows-1251, Windows-1252, Windows-1254', true); | |
if (!$detected) { | |
$msg = 'Unable to detect encoding of ' . var_export($string, true); | |
if ($throw_exceptions) { | |
throw new RuntimeException($msg); | |
} else { | |
trigger_error($msg, E_USER_ERROR); | |
} | |
} elseif (!in_array($detected, array('UTF-8', 'ASCII'))) { | |
$string = mb_convert_encoding($string, 'UTF-8', $detected); | |
} | |
} elseif (!is_scalar($string) && $string) { | |
$msg = 'Can not convert to string, expecting object with __toString, array or string: ' . var_export($string, true); | |
if ($throw_exceptions) { | |
throw new RuntimeException($msg); | |
} else { | |
trigger_error($msg, E_USER_ERROR); | |
} | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment