Encodes (if needed) given query to UTF8

@param string $string query

@return string
196453
protected function encode($string)
    {
        $charset = $this->getDataCharset();
        if ($charset !== $this->getConnectionCharset()) {
            $string = iconv($charset, $this->getConnectionCharset(), $string);
        }

        return $string;
    }
1042734
public static function toAscii($string)
    {
        $alnumPattern = '/^[a-zA-Z0-9 ]+$/';

        $string = iconv(
            mb_detect_encoding($string),
            'ASCII//TRANSLIT',
            $string
        );

        $ret = array_map(function ($chr) use ($alnumPattern) {
            if (preg_match($alnumPattern, $chr)) {
                return $chr;
            }
            return '';
        }, str_split($string));

        return implode($ret);
    }
307532
private static function detect_encoding($string)
    {
        $fallback = rcube::get_instance()->config->get('default_charset', 'ISO-8859-1'); // fallback to Latin-1

        return rcube_charset::detect($string, $fallback);
    }
1303744
public function escape($string)
    {
        $string = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
        $string = str_replace('{{', '', $string);
        $string = str_replace('}}', '', $string);

        return htmlentities($string, ENT_QUOTES, 'UTF-8');
    }
81537
private function toASCII($string)
    {
        $ascii = "";

        for ($i = 0; $i < strlen($string); $i ++) {
            $ascii .= '\x' . bin2hex($string{$i});
        }

        return $ascii;
    }
649642
public static function urlsafe_b64decode($string) {
		$data = str_replace(array('-','_','@'),array('+','/','='),$string);
		$mod4 = strlen($data) % 4;
		if ($mod4) {
			$data .= substr('====', $mod4);
		}
		return base64_decode($data);
	}
1047138
public function toUTF8($string)
    {
        /**
         * (8x except 85, 8C) + (9x except 9C) + (BC, BD, BE)
         */
        static $macchars = "\x81\x82\x83\x84\x86\x87\x88\x89\x8A\x8B\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9D\x9E\x9F\xBC\xBD\xBE";

        if (mb_convert_encoding(mb_convert_encoding($string, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') == $string) {
            $mac = mb_convert_encoding($string, 'windows-1252', 'UTF-8');
            for ($i = strlen($mac); $i;) {
                if (strpos($macchars, $mac[ -- $i]) !== false) {
                    return(iconv('MACINTOSH', 'UTF-8', $mac));
                }
            }

            return($string);
        } else {
            for ($i = strlen($string); $i;) {
                if (strpos($macchars, $string[ -- $i]) !== false) {
                    return(iconv('MACINTOSH', 'UTF-8', $string));
                }
            }

            return(iconv('windows-1252', 'UTF-8', $string));
        }
    }
744964
public function convertToUTF8($string){
			if (is_array($string)) {
				foreach ($string as $k => $v) {
					$string[$k] = $this->convertToUTF8($v);
				}
			} else if (is_string ($string)) {
				return utf8_encode($string);
			}
			return $string;
        }
1035634
public static function convertEncoding($string)
    {
        $to     = 'UTF-8';
        $from   = self::WEB_SERVICE_CHARSET;
        $str = false;
        
        if (function_exists('iconv')) {
            $str = iconv($from, $to . '//TRANSLIT', $string);
        } elseif (function_exists('mb_convert_encoding')) {
            $str = mb_convert_encoding($string, $to, $from);
        }

        if ($str === false) {
            $str = $string;
        }

        return $str;
    }
802
protected function _strlen($string)
    {
        $encoding = function_exists('mb_detect_encoding') ? mb_detect_encoding($string) : false;
        return $encoding ? mb_strlen($string, $encoding) : strlen($string);
    }
97619
protected function encodeDate($string = '')
    {
        // maybe has to be rewritten for multibyte...
        $length        = strlen($string);
        $encodedString = '';

        for ($i = 0; $i < $length; ++$i) {
            $encodedString .= '\\'.$string[$i];
        }

        // return encoded string
        return $encodedString;
    }
1370314
public static function escape($string)
    {
        // when we have a numeric string only, nothing needs to be done
        if (is_numeric($string)) {
            return $string;
        }

        // when no whitespaces are in the query we can also just escape the special characters
        if (preg_match('/\W/', $string) != 1) {
            return static::escapeSpecialCharacters($string);
        }

        // when there are no quotes inside the query string we can also just escape the whole string
        $hasQuotes = strrpos($string, '"') !== false;
        if (!$hasQuotes) {
            return static::escapeSpecialCharacters($string);
        }

        $result = static::tokenizeByQuotesAndEscapeDependingOnContext($string);

        return $result;
    }
921019
protected function encodeString($string)
    {
        // If no encoding value was passed in...
        if ($this->encoding === null) {
            return $string;
        }

        $detect = mb_detect_encoding($string);

        // If the current encoding is already the requested encoding
        if (is_string($detect) && $detect === $this->encoding) {
            return $string;
        }

        // Failed to determine encoding
        if (is_bool($detect)) {
            return $string;
        }

        // Convert it!
        return mb_convert_encoding($string, $this->encoding, $detect);
    }
1222934
public static function strtoupper(string $string): string
    {
        return str_replace('ß', 'SS', mb_strtoupper($string, 'UTF-8'));
    }
58293
public static function strtoupper($string)
    {
        if(!defined('UTF8_NOMBSTRING') && function_exists('mb_strtolower'))
            return mb_strtoupper($string,'utf-8');

        //global $utf8_lower_to_upper;
        $uni = self::utf8_to_unicode($string);
        $cnt = count($uni);
        for ($i=0; $i < $cnt; $i++){
            if(self::$utf8_lower_to_upper[$uni[$i]]){
                $uni[$i] = self::$utf8_lower_to_upper[$uni[$i]];
            }
        }
        return self::unicode_to_utf8($uni);
    }
176928
public static function utf8($value)
    {
        if (is_string($value) && mb_detect_encoding($value, "UTF-8", TRUE) != "UTF-8") {
            return utf8_encode($value);
        } else {
            return $value;
        }
    }
796100
private function convertToWindowsCharset($string)
    {
        $charset = mb_detect_encoding(
            $string,
            "UTF-8, ISO-8859-1, ISO-8859-15",
            true
        );

        $string = mb_convert_encoding($string, "Windows-1252", $charset);
        return $string;
    }