Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add Str::headline() #39174

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,27 @@ public static function title($value)
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
}

/**
* Convert the given string to title case for each word.
*
* @param string $value
* @return string
*/
public static function headline($value)
{
$parts = explode('_', static::replace(' ', '_', $value));

if (count($parts) > 1) {
$parts = array_map([static::class, 'title'], $parts);
}

$studly = static::studly(implode($parts));

$words = preg_split('/(?=[A-Z])/', $studly, -1, PREG_SPLIT_NO_EMPTY);

return implode(' ', $words);
}

/**
* Get the singular form of an English word.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,16 @@ public function title()
return new static(Str::title($this->value));
}

/**
* Convert the given string to title case for each word.
*
* @return static
*/
public function headline()
{
return new static(Str::headline($this->value));
}

/**
* Get the singular form of an English word.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ public function testStringTitle()
$this->assertSame('Jefferson Costella', Str::title('jefFErson coSTella'));
}

public function testStringHeadline()
{
$this->assertSame('Jefferson Costella', Str::headline('jefferson costella'));
$this->assertSame('Jefferson Costella', Str::headline('jefFErson coSTella'));
$this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses-_Laravel'));
$this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses__Laravel'));

$this->assertSame('Laravel P H P Framework', Str::headline('laravel_p_h_p_framework'));
$this->assertSame('Laravel P H P Framework', Str::headline('laravel _p _h _p _framework'));
$this->assertSame('Laravel Php Framework', Str::headline('laravel_php_framework'));
$this->assertSame('Laravel Ph P Framework', Str::headline('laravel-phP-framework'));
$this->assertSame('Laravel Php Framework', Str::headline('laravel -_- php -_- framework '));

$this->assertSame('Foo Bar', Str::headline('fooBar'));
$this->assertSame('Foo Bar', Str::headline('foo_bar'));
$this->assertSame('Foo Bar Baz', Str::headline('foo-barBaz'));
$this->assertSame('Foo Bar Baz', Str::headline('foo-bar_baz'));
}

public function testStringWithoutWordsDoesntProduceError()
{
$nbsp = chr(0xC2).chr(0xA0);
Expand Down