Skip to content

Commit 6791576

Browse files
authored
[8.x] Add Str::headline() (#39174)
* Add `Str::studlyWords()` * Rename method to `headline` and alter logic to properly handle title test cases * Add more assertions Co-authored-by: Steve Bauman <[email protected]>
1 parent fea2e54 commit 6791576

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,27 @@ public static function title($value)
679679
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
680680
}
681681

682+
/**
683+
* Convert the given string to title case for each word.
684+
*
685+
* @param string $value
686+
* @return string
687+
*/
688+
public static function headline($value)
689+
{
690+
$parts = explode('_', static::replace(' ', '_', $value));
691+
692+
if (count($parts) > 1) {
693+
$parts = array_map([static::class, 'title'], $parts);
694+
}
695+
696+
$studly = static::studly(implode($parts));
697+
698+
$words = preg_split('/(?=[A-Z])/', $studly, -1, PREG_SPLIT_NO_EMPTY);
699+
700+
return implode(' ', $words);
701+
}
702+
682703
/**
683704
* Get the singular form of an English word.
684705
*

src/Illuminate/Support/Stringable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,16 @@ public function title()
596596
return new static(Str::title($this->value));
597597
}
598598

599+
/**
600+
* Convert the given string to title case for each word.
601+
*
602+
* @return static
603+
*/
604+
public function headline()
605+
{
606+
return new static(Str::headline($this->value));
607+
}
608+
599609
/**
600610
* Get the singular form of an English word.
601611
*

tests/Support/SupportStrTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ public function testStringTitle()
3535
$this->assertSame('Jefferson Costella', Str::title('jefFErson coSTella'));
3636
}
3737

38+
public function testStringHeadline()
39+
{
40+
$this->assertSame('Jefferson Costella', Str::headline('jefferson costella'));
41+
$this->assertSame('Jefferson Costella', Str::headline('jefFErson coSTella'));
42+
$this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses-_Laravel'));
43+
$this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses__Laravel'));
44+
45+
$this->assertSame('Laravel P H P Framework', Str::headline('laravel_p_h_p_framework'));
46+
$this->assertSame('Laravel P H P Framework', Str::headline('laravel _p _h _p _framework'));
47+
$this->assertSame('Laravel Php Framework', Str::headline('laravel_php_framework'));
48+
$this->assertSame('Laravel Ph P Framework', Str::headline('laravel-phP-framework'));
49+
$this->assertSame('Laravel Php Framework', Str::headline('laravel -_- php -_- framework '));
50+
51+
$this->assertSame('Foo Bar', Str::headline('fooBar'));
52+
$this->assertSame('Foo Bar', Str::headline('foo_bar'));
53+
$this->assertSame('Foo Bar Baz', Str::headline('foo-barBaz'));
54+
$this->assertSame('Foo Bar Baz', Str::headline('foo-bar_baz'));
55+
}
56+
3857
public function testStringWithoutWordsDoesntProduceError()
3958
{
4059
$nbsp = chr(0xC2).chr(0xA0);

0 commit comments

Comments
 (0)