public static function convertToUTF8($str)
    {
        $encoding = mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1', 'WINDOWS-1252'], true);

        switch ($encoding) {
            case 'ISO-8859-1':
                $newStr = utf8_encode($str);
                break;
            /* Unreachable code. Not sure yet how we can improve this
             * situation.
            case 'WINDOWS-1252' :
                $newStr = iconv('cp1252', 'UTF-8', $str);
                break;
             */
            default:
                 $newStr = $str;
        }

        // Removing any control characters
        return preg_replace('%(?:[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%', '', $newStr);
    }
public static function convertToUTF8($str)
    {
        $encoding = mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1', 'WINDOWS-1252'], true);

        switch ($encoding) {
            case 'ISO-8859-1':
                $newStr = utf8_encode($str);
                break;
            /* Unreachable code. Not sure yet how we can improve this
             * situation.
            case 'WINDOWS-1252' :
                $newStr = iconv('cp1252', 'UTF-8', $str);
                break;
             */
            default:
                 $newStr = $str;
        }

        // Removing any control characters
        return preg_replace('%(?:[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%', '', $newStr);
    }
public static function convertToUTF8 ( $ str ) { $ encoding = mb_detect_encoding ( $ str , [ 'UTF-8' , 'ISO-8859-1' , 'WINDOWS-1252' ] , true ) ; switch ( $ encoding ) { case 'ISO-8859-1' : $ newStr = utf8_encode ( $ str ) ; break ; /* Unreachable code. Not sure yet how we can improve this
             * situation.
            case 'WINDOWS-1252' :
                $newStr = iconv('cp1252', 'UTF-8', $str);
                break;
             */ default : $ newStr = $ str ; } // Removing any control characters return preg_replace ( '%(?:[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%' , '' , $ newStr ) ; }
This method tries its best to convert the input string to UTF-8.

Currently only ISO-5991-1 input and UTF-8 input is supported, but this
may be expanded upon if we receive other examples.

@param string $str

@return string
This method tries its best to convert the input string to UTF - 8 .