Skip to content

Instantly share code, notes, and snippets.

@dominikzogg
Created January 8, 2012 14:32
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 dominikzogg/1578542 to your computer and use it in GitHub Desktop.
Save dominikzogg/1578542 to your computer and use it in GitHub Desktop.
php utf-8 encoding function
<?php
function encode_to_utf8 ($input) {
$output = $input;
if($encoding == get_encoding($input)) {
$output = iconv($encoding, "UTF-8", $output);
}
return($output);
}
function get_encoding ($input) {
$encondings = array("ASCII", "UTF-8", "ISO-8859-1");
$input_md5 = md5($input);
foreach($encondings as $enconding) {
$sample = iconv($enconding, $enconding, $input);
if(md5($sample) == $input_md5) {
return($enconding);
}
}
return(false);
}
function recursive_encode_to_utf8 ($array) {
if(is_array($array)) {
foreach($array as $key => $value) {
if(is_array($value)) {
$tmparray[$key] = recursive_encode_to_utf8($value);
}
else {
$tmparray[$key] = encode_to_utf8($value);
}
}
return($tmparray);
}
else {
return(encode_to_utf8($array));
}
}
@rjaeckel
Copy link

line 5 is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment