You are here

public static function Utf8::chr in Extensible BBCode 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Utf8.php \Drupal\xbbcode\Utf8::chr()

Encodes a numeric code point to a UTF-8 string.

Parameters

int $code: A single unicode code point (an integer between 0 ... 0x10ffff).

Return value

string The UTF-8 string, or an empty string for invalid code points.

1 call to Utf8::chr()
Utf8::decode in src/Utf8.php
Decode \uXXXX and \UXXXXXXXX sequences in a UTF8 string.

File

src/Utf8.php, line 65

Class

Utf8
Implementation of UTF-8 character utilities.

Namespace

Drupal\xbbcode

Code

public static function chr(int $code) : string {

  // Code point must be non-negative.
  if ($code < 0) {
    return '';
  }

  // Single byte (0xxxxxxx).
  if ($code < 0x80) {
    return chr($code);
  }

  // Two bytes (110xxxxx 10xxxxxx).
  if ($code < 0x800) {
    return chr(0xc0 | $code >> 6) . chr(0x80 | 0x3f & $code);
  }

  // Three bytes (1110xxxx 10xxxxxx 10xxxxxx).
  if ($code < 0x10000) {
    return chr(0xe0 | $code >> 12) . chr(0x80 | 0x3f & $code >> 6) . chr(0x80 | 0x3f & $code);
  }

  // Four bytes (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx).
  if ($code < 0x110000) {
    return chr(0xf0 | $code >> 18) . chr(0x80 | 0x3f & $code >> 12) . chr(0x80 | 0x3f & $code >> 6) . chr(0x80 | 0x3f & $code);
  }

  // Code point must be less than or equal to 0x10ffff.
  return '';
}