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 chunk() to Fluent Strings #35038

Merged
merged 1 commit into from
Nov 2, 2020
Merged

[8.x] Add chunk() to Fluent Strings #35038

merged 1 commit into from
Nov 2, 2020

Conversation

PHLAK
Copy link
Contributor

@PHLAK PHLAK commented Oct 31, 2020

Adds the chunk() method to fluent strings allowing a string to be "chunked" by a specified length and returning a collection of those chunks.

Example Usage

Str::of('foobarbaz')->chunk(3); // Returns collect(['foo', 'bar', 'baz'])

This is particularly useful when chaining additional methods.

Str::of('FooBarBaz')->lower()->chunk(3)->implode('-'); // Returns 'foo-bar-baz'

I decided to just open a PR instead of starting a discussion first because it was such a minor (non-breaking) change. Totally understand if this is not wanted.

@bonzai
Copy link
Contributor

bonzai commented Oct 31, 2020

I think it would be better to use mb_str_split, because str_split can't handle multibyte strings correctly:

   str_split('abc', 3)  // ["abc"]
mb_str_split('abc', 3)  // ["abc"]

   str_split('고마워', 3) // ["", "", ""]
mb_str_split('고마워', 3) // ["고마워"]

   str_split('고마워', 2) // [b"ê³", b" ë", b"§ˆ", b"ì›", b"Œ"]
mb_str_split('고마워', 2) // ["고마", ""]

@PHLAK
Copy link
Contributor Author

PHLAK commented Oct 31, 2020

@bonzai Sure, I'll update that.

EDIT: Done

@driesvints driesvints changed the title Add chunk() to Fluent Strings [8.x] Add chunk() to Fluent Strings Oct 31, 2020
@taylorotwell taylorotwell merged commit 7b7f420 into laravel:8.x Nov 2, 2020
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.

3 participants