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 
setStatus(int $status) deprecated

Sets the value for multibyte support status for the current 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 int
strlen(string $text) deprecated

Counts the number of characters in a UTF-8 string.

static string
strtoupper(string $text) deprecated

Converts a UTF-8 string to uppercase.

static string
strtolower(string $text) deprecated

Converts a UTF-8 string to lowercase.

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
substr(string $text, int $start, int $length = NULL) deprecated

Cuts off a piece of a string based on character indices and counts.

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)

Encodes MIME/HTTP headers that contain incorrectly encoded characters.

static string
mimeHeaderDecode(string $header)

Decodes MIME/HTTP encoded header values.

static string
caseFlip(array $matches) deprecated

Flip U+C0-U+DE to U+E0-U+FD and back. Can be used as preg_replace callback.

static bool
validateUtf8(string $text)

Checks whether a string is valid UTF-8.

static int|false
strpos(string $haystack, string $needle, int $offset = 0) deprecated

Finds the position of the first occurrence of a string in another string.

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 setStatus(int $status) deprecated

deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. In Drupal 9 there will be no way to set the status and in Drupal 8 this ability has been removed because mb_*() functions are supplied using Symfony's polyfill.

Sets the value for multibyte support status for the current environment.

The following status keys are supported:

  • \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.

Parameters

int $status

The new status of multibyte support.

See also

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

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 int strlen(string $text) deprecated

deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use mb_strlen() instead.

Counts the number of characters in a UTF-8 string.

This is less than or equal to the byte count.

Parameters

string $text

The string to run the operation on.

Return Value

int

The length of the string.

See also

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

static string strtoupper(string $text) deprecated

deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use mb_strtoupper() instead.

Converts a UTF-8 string to uppercase.

Parameters

string $text

The string to run the operation on.

Return Value

string

The string in uppercase.

See also

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

static string strtolower(string $text) deprecated

deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use mb_strtolower() instead.

Converts a UTF-8 string to lowercase.

Parameters

string $text

The string to run the operation on.

Return Value

string

The string in lowercase.

See also

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

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 substr(string $text, int $start, int $length = NULL) deprecated

deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use mb_substr() instead.

Cuts off a piece of a string based on character indices and counts.

Follows the same behavior as PHP's own substr() function. Note that for cutting off a string at a known character/substring location, the usage of PHP's normal strpos/substr is safe and much faster.

Parameters

string $text

The input string.

int $start

The position at which to start reading.

int $length

The number of characters to read.

Return Value

string

The shortened string.

See also

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

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)

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.

static string mimeHeaderDecode(string $header)

Decodes MIME/HTTP encoded header values.

Parameters

string $header

The header to decode.

Return Value

string

The mime-decoded header.

static string caseFlip(array $matches) deprecated

deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. There is no direct replacement.

Flip U+C0-U+DE to U+E0-U+FD and back. Can be used as preg_replace callback.

Parameters

array $matches

An array of matches by preg_replace_callback().

Return Value

string

The flipped text.

See also

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

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.

static int|false strpos(string $haystack, string $needle, int $offset = 0) deprecated

deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use mb_strpos() instead.

Finds the position of the first occurrence of a string in another string.

Parameters

string $haystack

The string to search in.

string $needle

The string to find in $haystack.

int $offset

If specified, start the search at this number of characters from the beginning (default 0).

Return Value

int|false

The position where $needle occurs in $haystack, always relative to the beginning (independent of $offset), or FALSE if not found. Note that a return value of 0 is not the same as FALSE.

See also

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