diff --git a/Tests/StringHelperTest.php b/Tests/StringHelperTest.php index 8d6fd578..e7f1197a 100644 --- a/Tests/StringHelperTest.php +++ b/Tests/StringHelperTest.php @@ -405,6 +405,9 @@ public static function seedTestUcwords(): array ['мога', 'Мога'], ['αβγ δεζ', 'Αβγ Δεζ'], ['åbc öde', 'Åbc Öde'], + 'form feed separator' => ["a\x0cbc def", "A\x0cBc Def"], + 'vertical tab separator' => ["a\x0bbc def", "A\x0bBc Def"], + 'several separators and leading whitespace' => [" \t\x0c åbc\x0c\x0cöde", " \t\x0c Åbc\x0c\x0cÖde"], ]; } diff --git a/src/StringHelper.php b/src/StringHelper.php index 98f73e7d..13ae3ed9 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -711,13 +711,10 @@ public static function ucwords($str) // Note: [\x0c\x09\x0b\x0a\x0d\x20] matches; // form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns // This corresponds to the definition of a "word" defined at http://www.php.net/ucwords - $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})[^\x0c\x09\x0b\x0a\x0d\x20]*/u'; + $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})([^\x0c\x09\x0b\x0a\x0d\x20]*)/u'; return preg_replace_callback($pattern, function ($matches) { - $leadingws = $matches[2]; - $ucfirst = mb_strtoupper($matches[3]); - $ucword = StringHelper::substr_replace(ltrim($matches[0]), $ucfirst, 0, 1); - return $leadingws . $ucword; + return $matches[2] . mb_strtoupper($matches[3]) . $matches[4]; }, $str); }