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

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

merged 3 commits into from
Oct 18, 2021

Conversation

stevebauman
Copy link
Contributor

@stevebauman stevebauman commented Oct 12, 2021

Description

This PR adds a new Stringable method to convert a string to Studly Words.

Purpose

  • There is studly() but it concatenates words together
  • There is title(), but it converts a series of words to ucfirst without separating words that are underscored, hyphenated, etc.

Use Case

This is super useful for displaying "base-named" PHP classes to user interfaces as titles:

// Displays: "Voice Recording Stored"
echo Str::studlyWords(
    class_basename(\App\Events\VoiceRecordingStored::class)
);

Or even taking slugs and converting them to a title:

$slug = "using-laravel-with-redis";

// Displays: "Using Laravel With Redis"
echo Str::studlyWords($slug);

Thanks for your time! No hard feelings on closure ❤️

@dennisprudlo
Copy link
Contributor

Can't we simply use ucwords?

@stevebauman
Copy link
Contributor Author

stevebauman commented Oct 12, 2021

No unfortunately. It doesn’t split words in a string that are concatenated (with an uppercase letter signifying a new word), hyphenated, or underscored.

You can take a look at the test cases for more examples 👍

@tontonsb
Copy link
Contributor

Shouldn't Str::title do this? It might be a breaking change, but that's the behaviour that I would expect from a "title case" converter.

@bert-w
Copy link
Contributor

bert-w commented Oct 13, 2021

I'm not sure what Str::title is supposed to do but it leaves in - and _. AFAIK it has very little to do with https://en.wikipedia.org/wiki/Title_case .

I have actually been using a custom function myself for a long time because I needed this functionality too:

function to_title($value)
{
    return ucwords(str_replace(['-', '_'], ' ', Str::kebab($value)));
}

In fact, you can change Str::kebab by Str::title or Str::snake and it will still work except for the weird test case:

'laravel  -_-  php   -_-   framework   '

which results in:

'Laravel    Php    Framework'

@stevebauman
Copy link
Contributor Author

@tontonsb

Shouldn't Str::title do this? It might be a breaking change, but that's the behaviour that I would expect from a "title case" converter.

This probably depends on what people interpret "title" as.

I think title should be left in tact for developers whom do not want their strings split by casing differences in their string.

For example:

// "I Ran To Mr.McDonalds"
Str::title("I ran to Mr.McDonalds");

// "I Ran To Mr. Mc Donalds"
Str::studlyWords("I ran to Mr.McDonalds");

@taylorotwell
Copy link
Member

taylorotwell commented Oct 14, 2021

I don't mind the concept but maybe we can refine the method name? Any thoughts on displayableTitle?

Hard for me to come up with a method name that reflects what this does in a word or two.

@stevebauman
Copy link
Contributor Author

stevebauman commented Oct 14, 2021

Yea absolutely! I don't mind displayableTitle() if you like it.

Here's a couple ideas as well (let me know your thoughts):

  • Str::human()
  • Str::humanTitle()
  • Str::readable()
  • Str::readableTitle()
  • Str::headline()

Str::title() and Str::headline() could be a cool looking separation of the two?

@tontonsb
Copy link
Contributor

Headline is cool. Memorable and fairly appropriate.

@taylorotwell
Copy link
Member

Yeah headline is a pretty good thought 👍

@stevebauman
Copy link
Contributor Author

Ok great -- let me update the PR with the name change. I'll comment back when it's ready 👍

@stevebauman stevebauman changed the title [8.x] Add Str::studlyWords() [8.x] Add Str::headline() Oct 15, 2021
@stevebauman
Copy link
Contributor Author

Ok it's ready to go 👍

I've updated the logic to conform to use title(), so developers can expect the same result when passing in a space-separated string (uppercasing each word as it should), but also parse non-space separated strings into words containing hyphens, underscores, or AlerternateCasing differences.

Let me know if you think this is okay, thanks!

@taylorotwell taylorotwell merged commit 6791576 into laravel:8.x Oct 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants