function Unicode::ucwords

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::ucwords()
  2. 8.9.x core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::ucwords()
  3. 10 core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::ucwords()

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.

Related topics

2 calls to Unicode::ucwords()
HandlerBase::caseTransform in core/modules/views/src/Plugin/views/HandlerBase.php
Transform a string by a certain method.
UnicodeTest::testUcwords in core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php
Tests multibyte ucwords.

File

core/lib/Drupal/Component/Utility/Unicode.php, line 264

Class

Unicode
Provides Unicode-related conversions and operations.

Namespace

Drupal\Component\Utility

Code

public static function ucwords($text) {
    $regex = '/(^|[' . static::PREG_CLASS_WORD_BOUNDARY . '])([^' . static::PREG_CLASS_WORD_BOUNDARY . '])/u';
    return preg_replace_callback($regex, function (array $matches) {
        return $matches[1] . mb_strtoupper($matches[2]);
    }, $text);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.