note 89688 modified in function.utf8-encode by danbrown
| From: | danbrown@php.net | Date: | Thu, 19 Mar 2009 13:13:45 +0000 |
| Subject: | note 89688 modified in function.utf8-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152139@lists.php.net to get a copy of this message | ||
This function may be useful do encode array keys and values [and checks first to see if it's
already in UTF format]:
<?php
public static 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;
}
?>
Hope this may help.
[NOTE BY danbrown AT php DOT net: Original function written by (cmyk777 AT gmail DOT com) on
28-JAN-09.]
--was--
The function from cmyk777 at gmail dot com does not work if the string ists already in UTF8 so I add
a check before convert
public static 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;
}
Hope this may help.
http://php.net/manual/en/function.utf8-encode.php