Convert a string to ascii or utf8 (using IDNA standard)

@param string  $input  Decoded e-mail address
@param boolean $is_utf Convert by idn_to_ascii if true and idn_to_utf8 if false

@return string Encoded e-mail address
974664
public static function idn_convert($input, $is_utf = false)
    {
        if ($at = strpos($input, '@')) {
            $user   = substr($input, 0, $at);
            $domain = substr($input, $at + 1);
        }
        else {
            $user   = '';
            $domain = $input;
        }

        // Note that in PHP 7.2/7.3 calling idn_to_* functions with default arguments
        // throws a warning, so we have to set the variant explicitely (#6075)
        $variant = defined('INTL_IDNA_VARIANT_UTS46') ? INTL_IDNA_VARIANT_UTS46 : null;
        $options = 0;

        // Because php-intl extension lowercases domains and return false
        // on invalid input (#6224), we skip conversion when not needed
        // for compatibility with our Net_IDNA2 wrappers in bootstrap.php

        if ($is_utf) {
            if (preg_match('/[^\x20-\x7E]/', $domain)) {
                $domain = idn_to_ascii($domain, $options, $variant);
            }
        }
        else if (preg_match('/(^|\.)xn--/i', $domain)) {
            $domain = idn_to_utf8($domain, $options, $variant);
        }

        if ($domain === false) {
            return '';
        }

        return $at ? $user . '@' . $domain : $domain;
    }
260430
protected static function Base64UrlEncode( $input ) {
			/**
			 * IMPORTANT NOTE:
			 * This is a hack suggested by @otto42 and @greenshady from
			 * the theme's review team. The usage of base64 for API
			 * signature encoding was approved in a Slack meeting
			 * held on Tue (10/25 2016).
			 *
			 * @todo Remove this hack once the base64 error is removed from the Theme Check.
			 *
			 * @since 1.2.2
			 * @author Vova Feldman (@svovaf)
			 */
			$fn = 'base64' . '_encode';
			$str = strtr( $fn( $input ), '+/', '-_' );
			$str = str_replace( '=', '', $str );

			return $str;
		}
1305240
public function canonicalize($input)
    {
        $strippedInput = str_replace(' ', '', $input);
        if (!preg_match('/^\d{4}[A-Za-z]{2}$/', $strippedInput)) {
            return $input;
        }

        return implode(' ', [substr($strippedInput, 0, 4), mb_strtoupper(substr($strippedInput, -2, 2))]);
    }
728364
protected function isString($input)
    {
        return is_scalar($input) || is_null($input) || (is_object($input) && method_exists($input, '__toString'));
    }
244249
public static function checkCode($input, $isCn = false, $verifyName = 'verifyCode')
    {
        $key = Cookie::get($verifyName);
        if (!$key) return false;
        $code = Model::getInstance()->cache()->get($key);
        Model::getInstance()->cache()->delete($key);
        $isCn && $input = md5(urldecode($input));
        if ($code === false || $code != $input) {
            return false;
        } else {
            return true;
        }
    }
854563
public static function convert(string $input, int $to) : string
    {
        $string = self::normalize($input);
        $parts = explode(' ', $string);
        array_walk($parts, function (&$part) {
            $part = ucfirst($part);
        });
        switch ($to) {
            case self::TYPE_NAMESPACE:
                return implode('\\', $parts);
            case self::TYPE_TABLE:
                return strtolower(implode('_', $parts));
            case self::TYPE_VARIABLE:
            case self::TYPE_ANGULAR_COMPONENT:
                return lcfirst(implode('', $parts));
            case self::TYPE_PATH:
                return implode('/', $parts);
            case self::TYPE_URL:
                return strtolower(implode('/', $parts));
            case self::TYPE_CSS_IDENTIFIER:
            case self::TYPE_ANGULAR_TAG:
                return strtolower(implode('-', $parts));
            case self::TYPE_ANGULAR_MODULE:
                return strtolower(implode('.', $parts));
            default:
                throw new DomainException("Please use one of the `TYPE_` constants on the `Codger\Generate\Language` class as 2nd parameter.");
        }
    }
959773
public static function charsetDecode($input,$_input_charset ,$_output_charset) {
        $output = "";
        if(!isset($_input_charset) )$_input_charset  = $_input_charset ;
        if($_input_charset == $_output_charset || $input ==null ) {
            $output = $input;
        } elseif (function_exists("mb_convert_encoding")) {
            $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
        } elseif(function_exists("iconv")) {
            $output = iconv($_input_charset,$_output_charset,$input);
        } else die("sorry, you have no libs support for charset changes.");
        return $output;
    }
1317404
public static function convert($input = STDIN)
    {
        $stream = fopen('php://temp', 'rw');

        foreach ($_SERVER as $key => $value) {
            if (0 === strpos($key, 'HTTP_')) {
                $key = str_replace('_', '-', strtolower(substr($key, 5)));
                fwrite($stream, "$key: $value\r\n");
            } elseif (in_array($key, ['CONTENT_LENGTH', 'CONTENT_MD5', 'CONTENT_TYPE'])) {
                $key = str_replace('_', '-', strtolower($key));
                fwrite($stream, "$key: $value\r\n");
            }
        }

        fwrite($stream, "\r\n");

        stream_copy_to_stream($input, $stream);

        rewind($stream);

        return new StreamedPart($stream);
    }
920119
public function encode($input)
    {
        $parts = explode('.', $input);

        foreach ($parts as &$part) {
            $part = $this->_encodePart($part);
        }

        return implode('.', $parts);
    }
483067
function _utf8ToChar(&$chars, &$idx)
	{
		if(($chars[$idx] >= 240) && ($chars[$idx] <= 255))
			/* 4 bytes */
			$utf =    ((int)($chars[$idx]-240)   << 18) +
				((int)($chars[++$idx]-128) << 12) +
				((int)($chars[++$idx]-128) << 6) +
				((int)($chars[++$idx]-128) << 0);
		else if (($chars[$idx] >= 224) && ($chars[$idx] <= 239))
			/* 3 bytes */
			$utf =    ((int)($chars[$idx]-224)   << 12) +
				((int)($chars[++$idx]-128) << 6) +
				((int)($chars[++$idx]-128) << 0);
		else if (($chars[$idx] >= 192) && ($chars[$idx] <= 223))
			/* 2 bytes */
			$utf =    ((int)($chars[$idx]-192)   << 6) +
				((int)($chars[++$idx]-128) << 0);
		else
			/* 1 byte */
			$utf = $chars[$idx];
		if(array_key_exists($utf,$this->utfMap))
			return \chr($this->utfMap[$utf]);
		else
			return "?";
	}
171341
public function encrypt($input, $key = '')
    {
        if (empty($input)) return $input;
        if (empty($key)) $key = md5($this->key);
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
        $ikey ="-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm";
        $nh1 = rand(0,64);
        $nh2 = rand(0,64);
        $nh3 = rand(0,64);
        $ch1 = $chars{$nh1};
        $ch2 = $chars{$nh2};
        $ch3 = $chars{$nh3};
        $nhnum = $nh1 + $nh2 + $nh3;
        $knum = 0;$i = 0;
        while(isset($key{$i})) $knum +=ord($key{$i++});
        $mdKey = substr(md5(md5(md5($key.$ch1).$ch2.$ikey).$ch3),$nhnum%8,$knum%8 + 16);
        $input = base64_encode(time().'_'.$input);
        $input = str_replace(array('+','/','='),array('-','_','.'),$input);
        $tmp = '';
        $j=0;$k = 0;
        $tlen = strlen($input);
        $klen = strlen($mdKey);
        for ($i=0; $i<$tlen; $i++) {
            $k = $k == $klen ? 0 : $k;
            $j = ($nhnum+strpos($chars,$input{$i})+ord($mdKey{$k++}))%64;
            $tmp .= $chars{$j};
        }
        $tmplen = strlen($tmp);
        $tmp = substr_replace($tmp,$ch3,$nh2 % ++$tmplen,0);
        $tmp = substr_replace($tmp,$ch2,$nh1 % ++$tmplen,0);
        $tmp = substr_replace($tmp,$ch1,$knum % ++$tmplen,0);
        return $tmp;
    }
1260504
function _ucs4_to_utf8($input)
    {
        $output = '';
        $k = 0;
        foreach ($input as $v) {
            ++$k;
            // $v = ord($v);
            if ($v < 128) { // 7bit are transferred literally
                $output .= chr($v);
            } elseif ($v < (1 << 11)) { // 2 bytes
                $output .= chr(192 + ($v >> 6)) . chr(128 + ($v & 63));
            } elseif ($v < (1 << 16)) { // 3 bytes
                $output .= chr(224 + ($v >> 12)) . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63));
            } elseif ($v < (1 << 21)) { // 4 bytes
                $output .= chr(240 + ($v >> 18)) . chr(128 + (($v >> 12) & 63))
                         . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63));
            } elseif ($v < (1 << 26)) { // 5 bytes
                $output .= chr(248 + ($v >> 24)) . chr(128 + (($v >> 18) & 63))
                         . chr(128 + (($v >> 12) & 63)) . chr(128 + (($v >> 6) & 63))
                         . chr(128 + ($v & 63));
            } elseif ($v < (1 << 31)) { // 6 bytes
                $output .= chr(252 + ($v >> 30)) . chr(128 + (($v >> 24) & 63))
                         . chr(128 + (($v >> 18) & 63)) . chr(128 + (($v >> 12) & 63))
                         . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63));
            } else {
                $this->_error('Conversion from UCS-4 to UTF-8 failed: malformed input at byte '.$k);
                return false;
            }
        }
        return $output;
    }
670745
protected static function &codePoints($input)
    {
        $codePoints = array(
            'all' => array(),
            'basic' => array(),
            'nonBasic' => array(),
        );

        $codes = &static::getCharsCodes($input);
        $codePoints['length'] = $length = count($codes);

        for ($i = 0; $i < $length; $i++) {
            $code = $codes[$i];

            if ($code < 128) {
                $codePoints['all'][] = $codePoints['basic'][] = $code;
            } else {
                $codePoints['all'][] = $codePoints['nonBasic'][] = $code;
            }
        }

        return $codePoints;
    }
1149146
public static function asciify(string $input, string $language = 'en', bool $removeUnsupported = true): string {
    $output = S::toAscii($input, $language, $removeUnsupported);

    return $output;
  }
800991
protected function _ucs4_to_utf8($input)
    {
        $output = '';
        foreach ($input as $k => $v) {
            if ($v < 128) { // 7bit are transferred literally
                $output .= chr($v);
            } elseif ($v < (1 << 11)) { // 2 bytes
                $output .= chr(192+($v >> 6)).chr(128+($v & 63));
            } elseif ($v < (1 << 16)) { // 3 bytes
                $output .= chr(224+($v >> 12)).chr(128+(($v >> 6) & 63)).chr(128+($v & 63));
            } elseif ($v < (1 << 21)) { // 4 bytes
                $output .= chr(240+($v >> 18)).chr(128+(($v >> 12) & 63)).chr(128+(($v >> 6) & 63)).chr(128+($v & 63));
            } elseif (self::$safe_mode) {
                $output .= self::$safe_char;
            } else {
                $this->_error('Conversion from UCS-4 to UTF-8 failed: malformed input at byte '.$k);
                return false;
            }
        }
        return $output;
    }
204062
public function convert($input)
    {
        if (is_string($input) === false) {
            throw new \InvalidArgumentException('Expected a string value at Parsedown converter.');
        }

        $converter = new \ParsedownExtra();

        return $converter->text($input);
    }
442934
public static function isBase64Encoded($input)
    {
        if ($input && @base64_encode(@base64_decode($input, true)) === $input) {
            return true;
        }

        return false;
    }