public static function mimeDecode( $text, $charset = 'utf-8' )
    {
        $origtext = $text;
        $text = @iconv_mime_decode( $text, 0, $charset );
        if ( $text !== false )
        {
            return $text;
        }

        // something went wrong while decoding, let's see if we can fix it
        // Try to fix lower case hex digits
        $text = preg_replace_callback(
            '/=(([a-f][a-f0-9])|([a-f0-9][a-f]))/',
            create_function( '$matches', 'return strtoupper($matches[0]);' ),
            $origtext
        );
        $text = @iconv_mime_decode( $text, 0, $charset );
        if ( $text !== false )
        {
            return $text;
        }

        // Workaround a bug in PHP 5.1.0-5.1.3 where the "b" and "q" methods
        // are not understood (but only "B" and "Q")
        $text = str_replace( array( '?b?', '?q?' ), array( '?B?', '?Q?' ), $origtext );
        $text = @iconv_mime_decode( $text, 0, $charset );
        if ( $text !== false )
        {
            return $text;
        }

        // Try it as latin 1 string
        $text = preg_replace( '/=\?([^?]+)\?/', '=?iso-8859-1?', $origtext );
        $text = iconv_mime_decode( $text, 0, $charset );

        return $text;
    }