/** * Converts a string from one charset to another. * * Works only if either the iconv or the mbstring extension * are present and best if both are available. * The original string is returned if conversion failed or none * of the extensions were available. * * @param mixed $input The data to be converted. If $input is an an array, * the array's values get converted recursively. * @param string $from The string's current charset. * @param string $to The charset to convert the string to. If not * specified, the global variable * $_HORDE_STRING_CHARSET will be used. * * @return mixed The converted input data. */ public function convertCharset($input, $from, $to = null) { /* Don't bother converting numbers. */ if (is_numeric($input)) { return $input; } /* Get the user's default character set if none passed in. */ if (is_null($to)) { $to = self::$charset; } /* If the from and to character sets are identical, return now. */ $from = Horde_String::lower($from); $to = Horde_String::lower($to); if ($from == $to) { return $input; } if (is_array($input)) { $tmp = array(); reset($input); while (list($key, $val) = each($input)) { $tmp[Horde_String::_convertCharset($key, $from, $to)] = Horde_String::convertCharset($val, $from, $to); } return $tmp; } if (is_object($input)) { $vars = get_object_vars($input); while (list($key, $val) = each($vars)) { $input->{$key} = Horde_String::convertCharset($val, $from, $to); } return $input; } if (!is_string($input)) { return $input; } return Horde_String::_convertCharset($input, $from, $to); }