Convert string from one encoding to another. First try mbstring, then iconv, finally strlen

@param string $value
@param string $to Encoding to convert to, e.g. 'UTF-8'
@param string $from Encoding to convert from, e.g. 'UTF-16LE'
@return string
966399
public static function ConvertEncoding($value, $to, $from)
	{
		if (self::getIsIconvEnabled()) {
			return iconv($from, $to, $value);
		}

		if (self::getIsMbstringEnabled()) {
			return mb_convert_encoding($value, $to, $from);
		}

		if($from == 'UTF-16LE'){
			return self::utf16_decode($value, false);
		}else if($from == 'UTF-16BE'){
			return self::utf16_decode($value);
		}
		// else, no conversion
		return $value;
	}
1105572
public static function transcode($source, $from_encoding, $to_encoding)
    {
        if (is_string($source)) {
            /*
             * "//TRANSLIT" is appendd to the $to_encoding to ensure that when iconv comes
             * across a character that cannot be represented in the target charset, it can
             * be approximated through one or several similarly looking characters.
             */
            return iconv($from_encoding, $to_encoding.'//TRANSLIT', $source);
        }
    }
1295471
public static function convertEncodingRecursive( $var, $to_encoding = NULL, $from_encoding = NULL )
    {
        $type = gettype($var);
        switch( $type ){
        case 'string':
            {
                return mb_convert_encoding($var,$to_encoding, $from_encoding);
            }
            break;
        case 'integer':
        case 'double':
        case 'boolean':
        case 'NULL':
        case 'unknown type':
            break;
        case 'array':
            {
                $newArray = array();
                foreach( $var as $key => $value ){
                    $value = self::convertEncodingRecursive( $value, $to_encoding, $from_encoding );
                    $newArray[ $key ] = $value;
                }
                return $newArray;
            }
            break;
        case 'object':
            {
                $newObject = clone $var;
                if ( $var instanceof \Traversable ){
                    foreach( $var as $key => $value ){
                        $value = self::convertEncodingRecursive( $value, $to_encoding, $from_encoding );
                        $newObject->$key = $value;
                    }
                    return $newObject;
                }
                else{
                    $obj_vars = get_object_vars( $var );
                    foreach( $obj_vars as $key => $value ){
                        $value = self::convertEncodingRecursive( $value, $to_encoding, $from_encoding );
                        $newObject->$key = $value;
                    }
                    return $newObject;
                }
            }
            break;
        }

        return $var;
    }
677466
public static function Base64Decode ($value, $options = []) {
		$value = base64_decode($value);

		$to_encoding = isset($options['to_encoding']) ? $options['to_encoding'] : false;
		if ($to_encoding !== false) {
			$from_encoding = isset($options['from_encoding']) ? $options['from_encoding'] : static::DEFAULT_ENCODING;
			return mb_convert_encoding($value, $to_encoding, $from_encoding);
		}

		return $value;
	}
1171748
public static function isCharset($value, $charset)
    {
        $available = \mb_list_encodings();

        $charset = \is_array($charset) ? $charset : array($charset);

        $charsetList = \array_filter(
            $charset,
            function ($charsetName) use ($available) {
                return \in_array($charsetName, $available, true);
            }
        );

        $detectedEncoding = \mb_detect_encoding($value, $charset, true);

        return \in_array($detectedEncoding, $charsetList, true);
    }
324829
protected function convert($from, $to, $value)
    {
        return ($value * $this->getConversionRate($from)) / $this->getConversionRate($to);
    }
876483
protected function assertValidStringLength($value, $fromPacket = true)
    {
        if (strlen($value) > 0xFFFF) {
            $this->throwException(
                sprintf(
                    'The string "%s" is longer than 65535 byte.',
                    substr($value, 0, 50)
                ),
                $fromPacket
            );
        }
    }
478241
public static function toString( $value, $with_type = FALSE, $max_size = self::TOSTRING_MAX_LENGTH, $tostring_methods = '__toString,toString' )
    {
        $ret = '';

        if ( $value === NULL ){
            $ret = 'NULL';
        }
        else{
            $type = gettype($value);
            switch( $type ){
            case 'string':
            case 'integer':
            case 'double':
            case 'boolean':
            case 'NULL':
            case 'unknown type':
                $ret = strval($value);
                if ( $with_type ){
                    $ret .= '(' . $type . ')';
                }
                break;
            case 'array':
                $ret = '';
                foreach( $value as $k => $v ){
                    if ( strlen($ret) > 0 )        $ret .= '/';
                    $ret .= "$k=" . self::toString( $v );
                    if ( $with_type ){
                        $ret .= '(' . gettype($v) . ')';
                    }
                }
                break;
            case 'object':
                {
                    $methods = explode( ',', $tostring_methods );
                    foreach( $methods as $method ){
                        if ( method_exists($value, $method) ){
                            $ret = $value->{$method}();
                            break;
                        }
                    }
                    if ( $with_type ){
                        $ret .= '(' . get_class($value) . ')';
                    }
                }
                break;
            }
        }

        if ( $max_size > 0 ){
            return strlen($ret) > $max_size ? substr($ret,0,$max_size) . '...' : $ret;
        }
        else{
            return $ret;
        }
    }
1196615
public static function encodeConvert($data, $from = 'gbk', $to = 'utf-8')
    {
        if (is_array($data)) {
            array_walk_recursive($data, function (&$data, $key) use ($from, $to) {
                $data = mb_convert_encoding($data, $to, $from);
            });
        } elseif (is_object($data)) {
            array_walk_recursive($data, function (&$data, $key) use ($from, $to) {
                $data = mb_convert_encoding($data, $to, $from);
            }, array('utf-8', 'gbk'));
        } else {
            $data = mb_convert_encoding($data, $to, $from);
        }

        return $data;
    }
1104880
public static function charset(String $str, String $fromCharset, String $toCharset = 'utf-8') : String
    {
        return mb_convert_encoding($str, $fromCharset, $toCharset);
    }
845167
protected function toString($value)
    {
        if (is_numeric($value)) {
            return "$value";
        }
        if ($value === true) {
            return 'true';
        }
        if ($value === false) {
            return 'false';
        }
        if (is_null($value)) {
            return '';
        }
        if ($this->getTypeCheck()->isArray($value) && count($value) === 1) {
            return $this->toString(reset($value));
        }

        return $value;
    }
317675
public static function commonConvertEncode($string, $to_encoding = 'utf-8', $utf8_first = true)
    {
        $encoding_list = [
            "ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5',
        ];

        if (! $utf8_first) {
            $encoding_list = [
                "ASCII", "GB2312", "GBK", 'UTF-8', 'BIG5',
            ];
        }

        $from_encoding = mb_detect_encoding($string, $encoding_list);

        return mb_convert_encoding(trim($string), $to_encoding, $from_encoding);
    }
37580
public function translate(string $from, string $to) : Text
    {
        return $this->pipe(
            function (string $value) use ($from, $to) : string {
                return strtr($value, $from, $to);
            }
        );
    }
50895
protected function encode($value)
    {
        $key = (string) $value;
        if (!isset(static::$cacheEncode[$key])) {
            $isAngularVar = false;
            if (\mb_substr($value, 0, 2) == '{{' && \mb_substr($value, -2) == '}}') {
                $isAngularVar = true;
                $value = \trim(\mb_substr($value, 2, -2));
            }
            static::$cacheEncode[$key] = \rawurlencode($value);
            static::$cacheEncode[$key] = \strtr(static::$cacheEncode[$key], static::$urlencodeCorrectionMap);
            if ($isAngularVar === true) {
                static::$cacheEncode[$key] = '{{ ' . static::$cacheEncode[$key] . ' }}';
            }
        }

        return static::$cacheEncode[$key];
    }
1287519
public static function convert($input, $from, $to)
    {
        if (!method_exists(self::class, $to)) {
            return $input;
        }
        
        if (in_array($from, ['camel', 'pascal'])) {
            $converted = self::$from($input);
            $converted = preg_replace('/([a-z])([A-Z])/', '$1 $2', $converted);
            $converted = preg_replace('/(\D)?(\d+)(\D)?/', '$1 $2 $3', $converted);
            return self::$to($converted);
        }
        
        if (method_exists(self::class, $from)) {
            return self::$to(self::$from($input));
        }
        
        return self::$to($input);
    }
915750
function convert_to_utf8($str, $encoding)
	{
		if (function_exists('iconv'))
		{
			$str = @iconv($encoding, 'UTF-8', $str);
		}
		elseif (function_exists('mb_convert_encoding'))
		{
			$str = @mb_convert_encoding($str, 'UTF-8', $encoding);
		}
		else
		{
			return FALSE;
		}

		return $str;
	}
851097
public function mbstring($fromOverride = null)
    {
        try {
            $converted = mb_convert_encoding($this->string, self::ENCODING, $fromOverride === null ? $this->encoding : $fromOverride);
        } catch (\Exception $e) {
            return false;
        }
        return $converted;
    }