/** * Convert a string from UTF-8 to any of various encodings * * @param string String to decode *[@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 decoded string or false on failure * @since 0.0.1 */ function decode_utf8($string = '', $encoding = 'iso-8859-1', $safe_mode = false) { $safe = $safe_mode ? $string : false; if (!$encoding) { $encoding = 'ISO-8859-1'; } if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') { return $string; } elseif (strtoupper($encoding) == 'ISO-8859-1') { return utf8_decode($string); } elseif (strtoupper($encoding) == 'WINDOWS-1252') { return map_iso8859_1_w1252(utf8_decode($string)); } elseif (strtoupper($encoding) == 'UNICODE-1-1-UTF-7') { $encoding = 'utf-7'; } if (function_exists('mb_convert_encoding')) { $conv = @mb_convert_encoding($string, strtoupper($encoding), 'UTF-8'); if ($conv) { return $conv; } } if (function_exists('iconv')) { $conv = @iconv('UTF-8', strtoupper($encoding), $string); if ($conv) { return $conv; } } if (function_exists('libiconv')) { $conv = @libiconv('UTF-8', strtoupper($encoding), $string); if ($conv) { return $conv; } } return $safe; }
/** * Converts a string from UTF-8 to any of various encodings * * @param string $string The string to decode * @param string $encoding The target encoding. (default='ISO-8859-1') * @param boolean $safeMode If set to TRUE, the original string is returned on errors * @return string Returns the decoded string or FALSE on error. * @since v0.1 */ function utf8Decode(string $string = '', string $encoding = 'iso-8859-1', bool $safeMode = false) : string { // Remember the string if we are in safe mode $safe = $safeMode ? $string : false; // Use default encoding if none is defined if (empty($encoding)) { $encoding = 'ISO-8859-1'; } $encodingUpper = \strtoupper($encoding); if ($encodingUpper == 'UTF-8' || $encodingUpper == 'UTF8') { return $string; } if ($encodingUpper == 'ISO-8859-1') { return \utf8_decode($string); } if ($encodingUpper == 'WINDOWS-1252') { return map_iso8859_1_w1252(\utf8_decode($string)); } if ($encodingUpper == 'UNICODE-1-1-UTF-7') { $encodingUpper = 'UTF-7'; } if (\function_exists('\\mb_convert_encoding')) { try { $conv = \mb_convert_encoding($string, $encodingUpper, 'UTF-8'); if ($conv) { return $conv; } } catch (\Throwable $ex) { $ex = null; } } if (\function_exists('\\iconv')) { try { $conv = \iconv('UTF-8', $encodingUpper, $string); if ($conv) { return $conv; } } catch (\Throwable $ex) { $ex = null; } } return $safe; }
/** * Convert iso-8859-1 to cp-1252 * * @param string $strValue * * @return string * * @see map_iso8859_1_w1252 */ public static function mapIso88591W1252($strValue = '') { return map_iso8859_1_w1252($strValue = ''); }