You are here

class NameUnicodeExtras in Name Field 8

Provides custom Unicode-related extension methods.

Hierarchy

Expanded class hierarchy of NameUnicodeExtras

1 file declares its use of NameUnicodeExtras
NameUnicodeExtrasTest.php in tests/src/Unit/NameUnicodeExtrasTest.php

File

src/NameUnicodeExtras.php, line 12

Namespace

Drupal\name
View source
class NameUnicodeExtras extends Unicode {

  /**
   * Split each word in a UTF-8 string.
   *
   * @param string $text
   *   The text that will be converted.
   *
   * @return array
   *   The input $text as an array of words.
   */
  public static function explode($text) {
    $regex = '/(^|[' . static::PREG_CLASS_WORD_BOUNDARY . '])/u';
    $words = preg_split($regex, $text, NULL, PREG_SPLIT_NO_EMPTY);
    return $words;
  }

  /**
   * Generate the initials of all first characters in a string.
   *
   * Note that this is case-insensitive, camel case words are treated as a
   * single word.
   *
   * @param string $text
   *   The text that will be converted.
   * @param string $delimitor
   *   An optional string to separate each character.
   *
   * @return string
   *   The input $text with first letters of each word capitalized.
   */
  public static function initials($text, $delimitor = '') {
    $text = mb_strtolower($text);
    $results = [];
    foreach (array_filter(self::explode($text)) as $word) {
      $results[] = mb_substr($word, 0, 1);
    }
    $text = implode($delimitor, $results);
    $text = mb_strtoupper($text);
    return $text ? $text . $delimitor : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NameUnicodeExtras::explode public static function Split each word in a UTF-8 string.
NameUnicodeExtras::initials public static function Generate the initials of all first characters in a string.
Unicode::caseFlip Deprecated public static function Flip U+C0-U+DE to U+E0-U+FD and back. Can be used as preg_replace callback.
Unicode::check public static function Checks for Unicode support in PHP and sets the proper settings if possible.
Unicode::convertToUtf8 public static function Converts data to UTF-8.
Unicode::encodingFromBOM public static function Decodes UTF byte-order mark (BOM) into the encoding's name.
Unicode::getStatus public static function Gets the current status of unicode/multibyte support on this environment.
Unicode::lcfirst public static function Converts the first character of a UTF-8 string to lowercase.
Unicode::mimeHeaderDecode public static function Decodes MIME/HTTP encoded header values.
Unicode::mimeHeaderEncode public static function Encodes MIME/HTTP headers that contain incorrectly encoded characters.
Unicode::PREG_CLASS_WORD_BOUNDARY constant Matches Unicode characters that are word boundaries.
Unicode::setStatus Deprecated public static function Sets the value for multibyte support status for the current environment.
Unicode::STATUS_ERROR constant Indicates an error during check for PHP unicode support.
Unicode::STATUS_MULTIBYTE constant Indicates that full unicode support with the PHP mbstring extension is being used.
Unicode::STATUS_SINGLEBYTE constant Indicates that standard PHP (emulated) unicode support is being used.
Unicode::strcasecmp public static function Compares UTF-8-encoded strings in a binary safe case-insensitive manner.
Unicode::strlen Deprecated public static function Counts the number of characters in a UTF-8 string.
Unicode::strpos Deprecated public static function Finds the position of the first occurrence of a string in another string.
Unicode::strtolower Deprecated public static function Converts a UTF-8 string to lowercase.
Unicode::strtoupper Deprecated public static function Converts a UTF-8 string to uppercase.
Unicode::substr Deprecated public static function Cuts off a piece of a string based on character indices and counts.
Unicode::truncate public static function Truncates a UTF-8-encoded string safely to a number of characters.
Unicode::truncateBytes public static function Truncates a UTF-8-encoded string safely to a number of bytes.
Unicode::ucfirst public static function Capitalizes the first character of a UTF-8 string.
Unicode::ucwords public static function Capitalizes the first character of each word in a UTF-8 string.
Unicode::validateUtf8 public static function Checks whether a string is valid UTF-8.