public static function decode(string $text, string $fromCharset): string
    {
        static $utf8Aliases = [
            'unicode-1-1-utf-8' => true,
            'utf8'              => true,
            'utf-8'             => true,
            'UTF8'              => true,
            'UTF-8'             => true,
        ];

        if (isset($utf8Aliases[$fromCharset])) {
            return $text;
        }

        $originalFromCharset  = $fromCharset;
        $lowercaseFromCharset = \strtolower($fromCharset);
        if (isset(self::$charsetAliases[$lowercaseFromCharset])) {
            $fromCharset = self::$charsetAliases[$lowercaseFromCharset];
        }

        \set_error_handler(static function () {});

        $iconvDecodedText = \iconv($fromCharset, 'UTF-8', $text);
        if (false === $iconvDecodedText) {
            $iconvDecodedText = \iconv($originalFromCharset, 'UTF-8', $text);
        }

        \restore_error_handler();

        if (false !== $iconvDecodedText) {
            return $iconvDecodedText;
        }

        $errorMessage = null;
        $errorNumber  = 0;
        \set_error_handler(static function ($nr, $message) use (&$errorMessage, &$errorNumber) {
            $errorMessage = $message;
            $errorNumber = $nr;
        });

        $decodedText = \mb_convert_encoding($text, 'UTF-8', $fromCharset);

        \restore_error_handler();

        if (null !== $errorMessage) {
            throw new UnsupportedCharsetException(\sprintf(
                'Unsupported charset "%s"%s: %s',
                $originalFromCharset,
                ($fromCharset !== $originalFromCharset) ? \sprintf(' (alias found: "%s")', $fromCharset) : '',
                $errorMessage
            ), $errorNumber);
        }

        return $decodedText;
    }
public static function decode(string $text, string $fromCharset): string
    {
        static $utf8Aliases = [
            'unicode-1-1-utf-8' => true,
            'utf8'              => true,
            'utf-8'             => true,
            'UTF8'              => true,
            'UTF-8'             => true,
        ];

        if (isset($utf8Aliases[$fromCharset])) {
            return $text;
        }

        $originalFromCharset  = $fromCharset;
        $lowercaseFromCharset = \strtolower($fromCharset);
        if (isset(self::$charsetAliases[$lowercaseFromCharset])) {
            $fromCharset = self::$charsetAliases[$lowercaseFromCharset];
        }

        \set_error_handler(static function () {});

        $iconvDecodedText = \iconv($fromCharset, 'UTF-8', $text);
        if (false === $iconvDecodedText) {
            $iconvDecodedText = \iconv($originalFromCharset, 'UTF-8', $text);
        }

        \restore_error_handler();

        if (false !== $iconvDecodedText) {
            return $iconvDecodedText;
        }

        $errorMessage = null;
        $errorNumber  = 0;
        \set_error_handler(static function ($nr, $message) use (&$errorMessage, &$errorNumber) {
            $errorMessage = $message;
            $errorNumber = $nr;
        });

        $decodedText = \mb_convert_encoding($text, 'UTF-8', $fromCharset);

        \restore_error_handler();

        if (null !== $errorMessage) {
            throw new UnsupportedCharsetException(\sprintf(
                'Unsupported charset "%s"%s: %s',
                $originalFromCharset,
                ($fromCharset !== $originalFromCharset) ? \sprintf(' (alias found: "%s")', $fromCharset) : '',
                $errorMessage
            ), $errorNumber);
        }

        return $decodedText;
    }
public static function decode ( string $ text , string $ fromCharset ) : string { static $ utf8Aliases = [ 'unicode-1-1-utf-8' => true , 'utf8' => true , 'utf-8' => true , 'UTF8' => true , 'UTF-8' => true , ] ; if ( isset ( $ utf8Aliases [ $ fromCharset ] ) ) { return $ text ; } $ originalFromCharset = $ fromCharset ; $ lowercaseFromCharset = \ strtolower ( $ fromCharset ) ; if ( isset ( self :: $ charsetAliases [ $ lowercaseFromCharset ] ) ) { $ fromCharset = self :: $ charsetAliases [ $ lowercaseFromCharset ] ; } \ set_error_handler ( static function ( ) { } ) ; $ iconvDecodedText = \ iconv ( $ fromCharset , 'UTF-8' , $ text ) ; if ( false === $ iconvDecodedText ) { $ iconvDecodedText = \ iconv ( $ originalFromCharset , 'UTF-8' , $ text ) ; } \ restore_error_handler ( ) ; if ( false !== $ iconvDecodedText ) { return $ iconvDecodedText ; } $ errorMessage = null ; $ errorNumber = 0 ; \ set_error_handler ( static function ( $ nr , $ message ) use ( & $ errorMessage , & $ errorNumber ) { $ errorMessage = $ message ; $ errorNumber = $ nr ; } ) ; $ decodedText = \ mb_convert_encoding ( $ text , 'UTF-8' , $ fromCharset ) ; \ restore_error_handler ( ) ; if ( null !== $ errorMessage ) { throw new UnsupportedCharsetException ( \ sprintf ( 'Unsupported charset "%s"%s: %s' , $ originalFromCharset , ( $ fromCharset !== $ originalFromCharset ) ? \ sprintf ( ' (alias found: "%s")' , $ fromCharset ) : '' , $ errorMessage ) , $ errorNumber ) ; } return $ decodedText ; }
Decode text to UTF-8.

@param string $text        Text to decode
@param string $fromCharset Original charset

@return string
Decode text to UTF - 8 .