/** * Convert a string from any of various encodings to UTF-8 * * @param string String to encode *[@param string Encoding; Default: ISO-8859-1] *[@param bool Safe Mode: if set to TRUE, the original string is retunred on errors] * @return string The encoded string or false on failure * @since 0.0.1 */ function encode_utf8($string = '', $encoding = 'iso-8859-1', $safe_mode = false) { $safe = $safe_mode ? $string : false; if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') { return $string; } elseif (strtoupper($encoding) == 'ISO-8859-1') { return utf8_encode($string); } elseif (strtoupper($encoding) == 'WINDOWS-1252') { return utf8_encode(map_w1252_iso8859_1($string)); } elseif (strtoupper($encoding) == 'UNICODE-1-1-UTF-7') { $encoding = 'utf-7'; } if (function_exists('mb_convert_encoding')) { $conv = @mb_convert_encoding($string, 'UTF-8', strtoupper($encoding)); if ($conv) { return $conv; } } if (function_exists('iconv')) { $conv = @iconv(strtoupper($encoding), 'UTF-8', $string); if ($conv) { return $conv; } } if (function_exists('libiconv')) { $conv = @libiconv(strtoupper($encoding), 'UTF-8', $string); if ($conv) { return $conv; } } return $safe; }
/** * Converts a string from any of various encodings to UTF-8. * * @param string $string The string to encode. * @param string $encoding The encoding of the string (default='ISO-8859-1') * @param boolean $safeMode If the safe mode is set to TRUE, the original string is retunred on errors. * @return string|FALSE Returns the encoded string, or FALSE on error * @since v0.1 */ function utf8Encode(string $string = '', string $encoding = 'iso-8859-1', bool $safeMode = false) : string { // Remember the string if we are in safe mode $safe = $safeMode ? $string : false; $encodingUpper = \strtoupper($encoding); if ($encodingUpper == 'UTF-8' || $encodingUpper == 'UTF8') { return $string; } if ($encodingUpper == 'ISO-8859-1') { return \utf8_encode($string); } if ($encodingUpper == 'WINDOWS-1252') { return \utf8_encode(map_w1252_iso8859_1($string)); } if ($encodingUpper == 'UNICODE-1-1-UTF-7') { $encodingUpper = 'UTF-7'; } if (\function_exists('\\mb_convert_encoding')) { try { $conv = \mb_convert_encoding($string, 'UTF-8', $encodingUpper); if ($conv) { return $conv; } } catch (\Exception $ex) { $ex = null; } } if (\function_exists('\\iconv')) { try { $conv = \iconv($encodingUpper, 'UTF-8', $string); if ($conv) { return $conv; } } catch (\Exception $ex) { $ex = null; } } return $safe; }
/** * Convert cp-1252 to iso-8859-1 * * @param string $strValue * * @return string * * @see map_w1252_iso8859_1 */ public static function mapW1252Iso88591($strValue = '') { return map_w1252_iso8859_1($strValue = ''); }