Skip to content

Instantly share code, notes, and snippets.

@dublado
Created December 14, 2011 20:52
Show Gist options
  • Save dublado/1478465 to your computer and use it in GitHub Desktop.
Save dublado/1478465 to your computer and use it in GitHub Desktop.
utf8 converter fixer
<pre><?php
$var = "Not%c3%adcias";
echo $var;
echo "\n";
echo htmlentities($var);
echo "\n";
echo urlencode($var);
echo "\n";
echo utf8_encode($var);
echo "\n";
echo to_utf8($var);
echo "\n";
function fixEncoding($in_str)
{
$cur_encoding = mb_detect_encoding($in_str) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
return $in_str;
else
return utf8_encode($in_str);
} // fixEncoding
function to_utf8($in)
{
if (is_array($in)) {
foreach ($in as $key => $value) {
$out[to_utf8($key)] = to_utf8($value);
}
} elseif(is_string($in)) {
if(mb_detect_encoding($in) != "UTF-8")
return utf8_encode($in);
else
return $in;
} else {
return $in;
}
return $out;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment