class Unicode (View source)

Provides Unicode-related conversions and operations.

Constants

PREG_CLASS_WORD_BOUNDARY

Matches Unicode characters that are word boundaries.

Characters with the following General_category (gc) property values are used as word boundaries. While this does not fully conform to the Word Boundaries algorithm described in http://unicode.org/reports/tr29, as PCRE does not contain the Word_Break property table, this simpler algorithm has to do.

  • Cc, Cf, Cn, Co, Cs: Other.
  • Pc, Pd, Pe, Pf, Pi, Po, Ps: Punctuation.
  • Sc, Sk, Sm, So: Symbols.
  • Zl, Zp, Zs: Separators.

Non-boundary characters include the following General_category (gc) property values:

  • Ll, Lm, Lo, Lt, Lu: Letters.
  • Mc, Me, Mn: Combining Marks.
  • Nd, Nl, No: Numbers.

Note that the PCRE property matcher is not used because we wanted to be compatible with Unicode 5.2.0 regardless of the PCRE version used (and any bugs in PCRE property tables).

STATUS_SINGLEBYTE

Indicates that standard PHP (emulated) unicode support is being used.

STATUS_MULTIBYTE

Indicates that full unicode support with the PHP mbstring extension is being used.

STATUS_ERROR

Indicates an error during check for PHP unicode support.

Methods

static int
getStatus()

Gets the current status of unicode/multibyte support on this environment.

static string
check()

Checks for Unicode support in PHP and sets the proper settings if possible.

static string|bool
encodingFromBOM(string $data)

Decodes UTF byte-order mark (BOM) into the encoding's name.

static string|bool
convertToUtf8(string $data, string $encoding)

Converts data to UTF-8.

static string
truncateBytes(string $string, int $len)

Truncates a UTF-8-encoded string safely to a number of bytes.

static string
ucfirst(string $text)

Capitalizes the first character of a UTF-8 string.

static string
lcfirst(string $text)

Converts the first character of a UTF-8 string to lowercase.

static string
ucwords(string $text)

Capitalizes the first character of each word in a UTF-8 string.

static string
truncate(string $string, int $max_length, bool $wordsafe = FALSE, bool $add_ellipsis = FALSE, int $min_wordsafe_length = 1)

Truncates a UTF-8-encoded string safely to a number of characters.

static int
strcasecmp(string $str1, string $str2)

Compares UTF-8-encoded strings in a binary safe case-insensitive manner.

static string
mimeHeaderEncode(string $string, bool $shorten = FALSE) deprecated

Encodes MIME/HTTP headers that contain incorrectly encoded characters.

static string
mimeHeaderDecode(string $header) deprecated

Decodes MIME/HTTP encoded header values.

static bool
validateUtf8(string $text)

Checks whether a string is valid UTF-8.

Details

static int getStatus()

Gets the current status of unicode/multibyte support on this environment.

Return Value

int

The status of multibyte support. It can be one of:

  • \Drupal\Component\Utility\Unicode::STATUS_MULTIBYTE Full unicode support using an extension.
  • \Drupal\Component\Utility\Unicode::STATUS_SINGLEBYTE Standard PHP (emulated) unicode support.
  • \Drupal\Component\Utility\Unicode::STATUS_ERROR An error occurred. No unicode support.

static string check()

Checks for Unicode support in PHP and sets the proper settings if possible.

Because of the need to be able to handle text in various encodings, we do not support mbstring function overloading. HTTP input/output conversion must be disabled for similar reasons.

Return Value

string

A string identifier of a failed multibyte extension check, if any. Otherwise, an empty string.

static string|bool encodingFromBOM(string $data)

Decodes UTF byte-order mark (BOM) into the encoding's name.

Parameters

string $data

The data possibly containing a BOM. This can be the entire contents of a file, or just a fragment containing at least the first five bytes.

Return Value

string|bool

The name of the encoding, or FALSE if no byte order mark was present.

static string|bool convertToUtf8(string $data, string $encoding)

Converts data to UTF-8.

Requires the iconv, GNU recode or mbstring PHP extension.

Parameters

string $data

The data to be converted.

string $encoding

The encoding that the data is in.

Return Value

string|bool

Converted data or FALSE.

static string truncateBytes(string $string, int $len)

Truncates a UTF-8-encoded string safely to a number of bytes.

If the end position is in the middle of a UTF-8 sequence, it scans backwards until the beginning of the byte sequence.

Use this function whenever you want to chop off a string at an unsure location. On the other hand, if you're sure that you're splitting on a character boundary (e.g. after using strpos() or similar), you can safely use substr() instead.

Parameters

string $string

The string to truncate.

int $len

An upper limit on the returned string length.

Return Value

string

The truncated string.

static string ucfirst(string $text)

Capitalizes the first character of a UTF-8 string.

Parameters

string $text

The string to convert.

Return Value

string

The string with the first character as uppercase.

static string lcfirst(string $text)

Converts the first character of a UTF-8 string to lowercase.

Parameters

string $text

The string that will be converted.

Return Value

string

The string with the first character as lowercase.

static string ucwords(string $text)

Capitalizes the first character of each word in a UTF-8 string.

Parameters

string $text

The text that will be converted.

Return Value

string

The input $text with each word capitalized.

static string truncate(string $string, int $max_length, bool $wordsafe = FALSE, bool $add_ellipsis = FALSE, int $min_wordsafe_length = 1)

Truncates a UTF-8-encoded string safely to a number of characters.

Parameters

string $string

The string to truncate.

int $max_length

An upper limit on the returned string length, including trailing ellipsis if $add_ellipsis is TRUE.

bool $wordsafe

If TRUE, attempt to truncate on a word boundary. Word boundaries are spaces, punctuation, and Unicode characters used as word boundaries in non-Latin languages; see Unicode::PREG_CLASS_WORD_BOUNDARY for more information. If a word boundary cannot be found that would make the length of the returned string fall within length guidelines (see parameters $max_length and $min_wordsafe_length), word boundaries are ignored.

bool $add_ellipsis

If TRUE, add '...' to the end of the truncated string (defaults to FALSE). The string length will still fall within $max_length.

int $min_wordsafe_length

If $wordsafe is TRUE, the minimum acceptable length for truncation (before adding an ellipsis, if $add_ellipsis is TRUE). Has no effect if $wordsafe is FALSE. This can be used to prevent having a very short resulting string that will not be understandable. For instance, if you are truncating the string "See myverylongurlexample.com for more information" to a word-safe return length of 20, the only available word boundary within 20 characters is after the word "See", which wouldn't leave a very informative string. If you had set $min_wordsafe_length to 10, though, the function would realise that "See" alone is too short, and would then just truncate ignoring word boundaries, giving you "See myverylongurl..." (assuming you had set $add_ellipses to TRUE).

Return Value

string

The truncated string.

static int strcasecmp(string $str1, string $str2)

Compares UTF-8-encoded strings in a binary safe case-insensitive manner.

Parameters

string $str1

The first string.

string $str2

The second string.

Return Value

int

Returns < 0 if $str1 is less than $str2; > 0 if $str1 is greater than $str2, and 0 if they are equal.

static string mimeHeaderEncode(string $string, bool $shorten = FALSE) deprecated

deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use \Symfony\Component\Mime\Header\UnstructuredHeader instead.

Encodes MIME/HTTP headers that contain incorrectly encoded characters.

For example, Unicode::mimeHeaderEncode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".

See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.

Notes:

  • Only encode strings that contain non-ASCII characters.
  • We progressively cut-off a chunk with self::truncateBytes(). This ensures each chunk starts and ends on a character boundary.
  • Using \n as the chunk separator may cause problems on some systems and may have to be changed to \r\n or \r.

Parameters

string $string

The header to encode.

bool $shorten

If TRUE, only return the first chunk of a multi-chunk encoded string.

Return Value

string

The mime-encoded header.

See also

https://www.drupal.org/node/3207439

static string mimeHeaderDecode(string $header) deprecated

deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use iconv_mime_decode() instead.

Decodes MIME/HTTP encoded header values.

Parameters

string $header

The header to decode.

Return Value

string

The mime-decoded header.

See also

https://www.drupal.org/node/3207439

static bool validateUtf8(string $text)

Checks whether a string is valid UTF-8.

All functions designed to filter input should use drupal_validate_utf8 to ensure they operate on valid UTF-8 strings to prevent bypass of the filter.

When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent bytes. When these subsequent bytes are HTML control characters such as quotes or angle brackets, parts of the text that were deemed safe by filters end up in locations that are potentially unsafe; An onerror attribute that is outside of a tag, and thus deemed safe by a filter, can be interpreted by the browser as if it were inside the tag.

The function does not return FALSE for strings containing character codes above U+10FFFF, even though these are prohibited by RFC 3629.

Parameters

string $text

The text to check.

Return Value

bool

TRUE if the text is valid UTF-8, FALSE if not.