Convert a single UTF-8 character into its decimal code point.

@param string $char

@return int
344428
public static function toUnicodeCodepoint($char)
    {
        $charCode = false;
        // 7bits, 1 char
        if ((ord($char[0]) & 0x80) == 0x00) {
            $charCode = ord($char[0]);
        } elseif ((ord($char[0]) & 0xe0) == 0xc0) { // 11 bits, 2 chars
            if ((ord($char[1]) & 0xc0) != 0x80) {
                return $charCode;
            }

            $charCode = (
                ((ord($char[0]) & 0x1f) << 6) +
                ((ord($char[1]) & 0x3f))
            );

            if ($charCode < 128) {
                throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
            }
        } elseif ((ord($char[0]) & 0xf0) == 0xe0) { // 16 bits, 3 chars
            if ((ord($char[1]) & 0xc0) != 0x80 ||
                 (ord($char[2]) & 0xc0) != 0x80) {
                return $charCode;
            }
            $charCode = (
                ((ord($char[0]) & 0x0f) << 12) +
                ((ord($char[1]) & 0x3f) << 6) +
                ((ord($char[2]) & 0x3f))
            );

            if ($charCode < 2048) {
                throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
            }
        } elseif ((ord($char[0]) & 0xf8) == 0xf0) { // 21 bits, 4 chars
            if ((ord($char[1]) & 0xc0) != 0x80 ||
                 (ord($char[2]) & 0xc0) != 0x80 ||
                 (ord($char[3]) & 0xc0) != 0x80) {
                return $charCode;
            }

            $charCode = (
                ((ord($char[0]) & 0x07) << 18) +
                ((ord($char[1]) & 0x3f) << 12) +
                ((ord($char[2]) & 0x3f) << 6) +
                ((ord($char[3]) & 0x3f))
            );

            if ($charCode < 65536) {
                throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
            }
        } elseif ((ord($char[0]) & 0xfc) == 0xf8) { // 26 bits, 5 chars
            if ((ord($char[1]) & 0xc0) != 0x80 ||
                 (ord($char[2]) & 0xc0) != 0x80 ||
                 (ord($char[3]) & 0xc0) != 0x80 ||
                 (ord($char[4]) & 0xc0) != 0x80) {
                return $charCode;
            }

            $charCode = (
                ((ord($char[0]) & 0x03) << 24) +
                ((ord($char[1]) & 0x3f) << 18) +
                ((ord($char[2]) & 0x3f) << 12) +
                ((ord($char[3]) & 0x3f) << 6) +
                ((ord($char[4]) & 0x3f))
            );

            if ($charCode < 2097152) {
                throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
            }
        } elseif ((ord($char[0]) & 0xfe) == 0xfc) { // 31 bits, 6 chars
            if ((ord($char[1]) & 0xc0) != 0x80 ||
                 (ord($char[2]) & 0xc0) != 0x80 ||
                 (ord($char[3]) & 0xc0) != 0x80 ||
                 (ord($char[4]) & 0xc0) != 0x80 ||
                 (ord($char[5]) & 0xc0) != 0x80) {
                return $charCode;
            }

            $charCode = (
                ((ord($char[0]) & 0x01) << 30) +
                ((ord($char[1]) & 0x3f) << 24) +
                ((ord($char[2]) & 0x3f) << 18) +
                ((ord($char[3]) & 0x3f) << 12) +
                ((ord($char[4]) & 0x3f) << 6) +
                ((ord($char[5]) & 0x3f))
            );

            if ($charCode < 67108864) {
                throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
            }
        }

        return $charCode;
    }
701517
private static int utf8Length(String string) {
    int stringLength = string.length();
    int utf8Length = 0;
    for (int i = 0; i < stringLength; i++) {
      int c = string.charAt(i);
      if (c <= 0x007F) {
        utf8Length++;
      } else if (c > 0x07FF) {
        utf8Length += 3;
      } else {
        utf8Length += 2;
      }
    }
    return utf8Length;
  }
966248
function utf8_encode($str, $charset) {

		if ($charset === 'utf-8') {
			return $str;
		}

			// Charset is case-insensitive.
		if ($this->initCharset($charset)) { // Parse conv. table if not already...
			$strLen = strlen($str);
			$outStr = '';

			for ($a = 0; $a < $strLen; $a++) { // Traverse each char in string.
				$chr = substr($str, $a, 1);
				$ord = ord($chr);
				if (isset($this->twoByteSets[$charset])) { // If the charset has two bytes per char
					$ord2 = ord($str{$a + 1});
					$ord = $ord << 8 | $ord2; // assume big endian

					if (isset($this->parsedCharsets[$charset]['local'][$ord])) { // If the local char-number was found in parsed conv. table then we use that, otherwise 127 (no char?)
						$outStr .= $this->parsedCharsets[$charset]['local'][$ord];
					} else {
						$outStr .= chr($this->noCharByteVal);
					} // No char exists
					$a++;
				} elseif ($ord > 127) { // If char has value over 127 it's a multibyte char in UTF-8
					if (isset($this->eucBasedSets[$charset])) { // EUC uses two-bytes above 127; we get both and advance pointer and make $ord a 16bit int.
						if ($charset != 'shift_jis' || ($ord < 0xA0 || $ord > 0xDF)) { // Shift-JIS: chars between 160 and 223 are single byte
							$a++;
							$ord2 = ord(substr($str, $a, 1));
							$ord = $ord * 256 + $ord2;
						}
					}

					if (isset($this->parsedCharsets[$charset]['local'][$ord])) { // If the local char-number was found in parsed conv. table then we use that, otherwise 127 (no char?)
						$outStr .= $this->parsedCharsets[$charset]['local'][$ord];
					} else {
						$outStr .= chr($this->noCharByteVal);
					} // No char exists
				} else {
					$outStr .= $chr;
				} // ... otherwise it's just ASCII 0-127 and one byte. Transparent
			}
			return $outStr;
		}
	}
80405
public static function toUTF8($str)
    {
        if (!is_null($str) && false === mb_detect_encoding($str, 'UTF-8', true)) {
            $str = utf8_encode($str);
        }

        return $str;
    }
287118
public static function toUTF8Character($charCode)
    {
        switch ($charCode) {
            case 0:
                $char = chr(0);
                break;
            case !($charCode & 0xffffff80): // 7 bit
                $char = chr($charCode);
                break;
            case !($charCode & 0xfffff800): // 11 bit
                $char = (
                    chr(0xc0 | (($charCode >> 6) & 0x1f)) .
                    chr(0x80 | ($charCode & 0x3f))
                );
                break;
            case !($charCode & 0xffff0000): // 16 bit
                $char = (
                    chr(0xe0 | (($charCode >> 12) & 0x0f)) .
                    chr(0x80 | (($charCode >> 6) & 0x3f)) .
                    chr(0x80 | ($charCode & 0x3f))
                );
                break;
            case !($charCode & 0xffe00000): // 21 bit
                $char = (
                    chr(0xf0 | (($charCode >> 18) & 0x07)) .
                    chr(0x80 | (($charCode >> 12) & 0x3f)) .
                    chr(0x80 | (($charCode >> 6) & 0x3f)) .
                    chr(0x80 | ($charCode & 0x3f))
                );
                break;
            case !($charCode & 0xfc000000): // 26 bit
                $char = (
                    chr(0xf8 | (($charCode >> 24) & 0x03)) .
                    chr(0x80 | (($charCode >> 18) & 0x3f)) .
                    chr(0x80 | (($charCode >> 12) & 0x3f)) .
                    chr(0x80 | (($charCode >> 6) & 0x3f)) .
                    chr(0x80 | ($charCode & 0x3f))
                );
                break;
            default: // 31 bit
                $char = (
                    chr(0xfc | (($charCode >> 30) & 0x01)) .
                    chr(0x80 | (($charCode >> 24) & 0x3f)) .
                    chr(0x80 | (($charCode >> 18) & 0x3f)) .
                    chr(0x80 | (($charCode >> 12) & 0x3f)) .
                    chr(0x80 | (($charCode >> 6) & 0x3f)) .
                    chr(0x80 | ($charCode & 0x3f))
                );
        }

        return $char;
    }
383025
public static function isLowerCase($char): bool
    {
        self::handleIncomingChar($char);

        return static::compare($char, static::toLowerCase($char)) === 0;
    }
308134
public function char(int $char = null): string
    {
        $pos = $this->pos;
        if ( ! is_null($char)) {
            $pos = $char;
        }

        if ( ! isset($this->content[$pos])) {
            return '';
        }

        return $this->content[$pos];
    }
375350
function utf8ToStr($utf)
	{
		$chars = unpack('C*', $utf);
		$cnt = \count($chars);
		$res = ""; /* No simple way to do it in place... concatenate char by char */
		for ($i=1;$i<=$cnt;$i++)
			$res .= $this->_utf8ToChar($chars, $i);
		return $res;
	}
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;
        }
353816
public function char($char = null)
    {
        $pos = $this->pos;
        if ( ! is_null($char)) {
            $pos = $char;
        }

        if ( ! isset($this->content[$pos])) {
            return '';
        }

        return $this->content[$pos];
    }
1135251
public static function ord(string $char): int
    {
        $char = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');

        return (int) unpack('Ncode', $char)['code'];
    }
450667
private function getLength(string $string) : ?int
    {
        if (extension_loaded('mbstring') && ! defined('NO_PHP_EXTENSIONS')) { // constant used for tests
            if (! mb_check_encoding($string, 'UTF-8')) {
                return null;
            }

            return mb_strlen($string, 'UTF-8');
        }

        $charLength = 0;

        $length = strlen($string);

        for ($i = 0; $i < $length; $i++) {
            $charLength++;

            $char = ord($string[$i]);

            if ($char < 0x80) { // 0bbbbbbb
                continue;
            }

            if (($char & 0xE0) === 0xC0) { // 110bbbbb
                $n = 1;
            } elseif (($char & 0xF0) === 0xE0) { // 1110bbbb
                $n = 2;
            } elseif (($char & 0xF0) === 0xF0) { // 11110bbb
                $n = 3;
            } else { // invalid char
                return null;
            }

            for ($j = 0; $j < $n; $j++) {
                if (++$i === $length) { // unexpected end of string
                    return null;
                }

                if ((ord($string[$i]) & 0xC0) !== 0x80) { // does not match 10bbbbbb
                    return null;
                }
            }
        }

        return $charLength;
    }
275436
private function index(string $char): int {
        if (isset($this->index[$char])) {
            return $this->index[$char];
        }

        $this->index_count++;
        $this->index[$char] = $this->index_count;
        return $this->index_count;
    }
249590
public static function ord(string $char): int
    {
        $h = \ord($char[0]);
        
        if ($h <= 0x7F) {
            return $h;
        }
        
        if ($h < 0xC2) {
            throw new \InvalidArgumentException('Could not get unicode codepoint for character: "' . $char . '"');
        }
        
        if ($h <= 0xDF) {
            return ($h & 0x1F) << 6 | (\ord($char[1]) & 0x3F);
        }
        
        if ($h <= 0xEF) {
            return (($h & 0x0F) << 12 | (\ord($char[1]) & 0x3F) << 6 | (\ord($char[2]) & 0x3F));
        }
        
        if ($h <= 0xF4) {
            return (($h & 0x0F) << 18 | (\ord($char[1]) & 0x3F) << 12 | (\ord($char[2]) & 0x3F) << 6 | (\ord($char[3]) & 0x3F));
        }
        
        throw new \InvalidArgumentException('Could not get unicode codepoint for character: "' . $char . '"');
    }
1051228
public static function toInt(string $str) : int
    {
        $result = 0;
        $str    = mb_convert_kana(self::replaceRoman($str), 'a');
        foreach (self::$romans as $key => $value) {
            while (mb_strpos($str, $key) === 0) {
                $result += $value;
                $str = mb_substr($str, strlen($key));
            }
        }
        return $result;
    }
994282
private static function utf8_to_unicode( &$str )
    {
        $unicode = array();
        $values = array();
        $looking_for = 1;

        for ($i = 0; $i < strlen( $str ); $i++ ) {
            $this_value = ord( $str[ $i ] );
            if ( $this_value < 128 ) $unicode[] = $this_value;
            else {
                if ( count( $values ) == 0 ) $looking_for = ( $this_value < 224 ) ? 2 : 3;
                $values[] = $this_value;
                if ( count( $values ) == $looking_for ) {
                    $number = ( $looking_for == 3 ) ?
                        ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
                        ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
                    $unicode[] = $number;
                    $values = array();
                    $looking_for = 1;
                }
            }
        }
        return $unicode;
    }
1338812
private function getCharIndex(string $char): int {
        if (\strlen($char) > 1) $char = \substr($char, 0, 1);
        return \ord($char) - \ord('a');
    }