src/helpers/StringHelper.php
public static function convertToUtf8(string $string): string
    {
        // If it's already a UTF8 string, just clean and return it
        if (static::isUtf8($string)) {
            return HtmlPurifier::cleanUtf8($string);
        }

        // Otherwise set HTMLPurifier to the actual string encoding
        $config = \HTMLPurifier_Config::createDefault();
        $config->set('Core.Encoding', static::encoding($string));

        // Clean it
        $string = HtmlPurifier::cleanUtf8($string);

        // Convert it to UTF8 if possible
        if (function_exists('iconv') && \HTMLPurifier_Encoder::testIconvTruncateBug() === \HTMLPurifier_Encoder::ICONV_OK) {
            $string = HtmlPurifier::convertToUtf8($string, $config);
        } else {
            $encoding = static::encoding($string);
            $string = mb_convert_encoding($string, 'utf-8', $encoding);
        }

        return $string;
    }
public static function convertToUtf8(string $string): string
    {
        // If it's already a UTF8 string, just clean and return it
        if (static::isUtf8($string)) {
            return HtmlPurifier::cleanUtf8($string);
        }

        // Otherwise set HTMLPurifier to the actual string encoding
        $config = \HTMLPurifier_Config::createDefault();
        $config->set('Core.Encoding', static::encoding($string));

        // Clean it
        $string = HtmlPurifier::cleanUtf8($string);

        // Convert it to UTF8 if possible
        if (function_exists('iconv') && \HTMLPurifier_Encoder::testIconvTruncateBug() === \HTMLPurifier_Encoder::ICONV_OK) {
            $string = HtmlPurifier::convertToUtf8($string, $config);
        } else {
            $encoding = static::encoding($string);
            $string = mb_convert_encoding($string, 'utf-8', $encoding);
        }

        return $string;
    }
public static function convertToUtf8 ( string $ string ) : string { // If it's already a UTF8 string, just clean and return it if ( static :: isUtf8 ( $ string ) ) { return HtmlPurifier :: cleanUtf8 ( $ string ) ; } // Otherwise set HTMLPurifier to the actual string encoding $ config = \ HTMLPurifier_Config :: createDefault ( ) ; $ config -> set ( 'Core.Encoding' , static :: encoding ( $ string ) ) ; // Clean it $ string = HtmlPurifier :: cleanUtf8 ( $ string ) ; // Convert it to UTF8 if possible if ( function_exists ( 'iconv' ) && \ HTMLPurifier_Encoder :: testIconvTruncateBug ( ) === \ HTMLPurifier_Encoder :: ICONV_OK ) { $ string = HtmlPurifier :: convertToUtf8 ( $ string , $ config ) ; } else { $ encoding = static :: encoding ( $ string ) ; $ string = mb_convert_encoding ( $ string , 'utf-8' , $ encoding ) ; } return $ string ; }
Attempts to convert a string to UTF-8 and clean any non-valid UTF-8 characters.

@param string $string
@return string
Attempts to convert a string to UTF - 8 and clean any non - valid UTF - 8 characters .