You are here

public static function Utf8::ord in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x src/Utf8.php \Drupal\xbbcode\Utf8::ord()

Finds the character code for a UTF-8 character: like ord() but for UTF-8.

Parameters

string $character: A single UTF-8 character.

Return value

int The character code, or -1 if an illegal character is found.

See also

\Drupal\Core\Transliteration\PhpTransliteration::ordUTF8()

1 call to Utf8::ord()
Utf8::encode in src/Utf8.php
Escape specified characters in a UTF8 string to \uXXXX and \UXXXXXXXX.

File

src/Utf8.php, line 27

Class

Utf8
Implementation of UTF-8 character utilities.

Namespace

Drupal\xbbcode

Code

public static function ord($character) : int {
  $first_byte = ord($character[0]);
  if (($first_byte & 0x80) === 0) {

    // Single-byte form: 0xxxxxxxx.
    return $first_byte;
  }
  if (($first_byte & 0xe0) === 0xc0) {

    // Two-byte form: 110xxxxx 10xxxxxx.
    return (($first_byte & 0x1f) << 6) + (ord($character[1]) & 0x3f);
  }
  if (($first_byte & 0xf0) === 0xe0) {

    // Three-byte form: 1110xxxx 10xxxxxx 10xxxxxx.
    return (($first_byte & 0xf) << 12) + ((ord($character[1]) & 0x3f) << 6) + (ord($character[2]) & 0x3f);
  }
  if (($first_byte & 0xf8) === 0xf0) {

    // Four-byte form: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx.
    return (($first_byte & 0x7) << 18) + ((ord($character[1]) & 0x3f) << 12) + ((ord($character[2]) & 0x3f) << 6) + (ord($character[3]) & 0x3f);
  }

  // Other forms are not legal.
  return -1;
}