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

[7.x] Add split() to Stringable class #32713

Merged
merged 2 commits into from
May 8, 2020
Merged

Conversation

tuarrep
Copy link
Contributor

@tuarrep tuarrep commented May 7, 2020

Add a split() method to Stringable as a proxy to preg_split() to allow splitting by regex in addition of explode()

Example

Str::of('hypertext language, programming')
    ->split('/[\s,]+/');

/*
Illuminate\Support\Collection {#3523
     all: [
       "hypertext",
       "language",
       "programming",
     ],
   }

*/

@GrahamCampbell
Copy link
Member

I think you will need to first add this method to the Str class.

*/
public function split($pattern, $limit = -1, $flags = 0)
{
$keywords = preg_split($pattern, $this->value, $limit, $flags);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about error handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preg_split returns false on failing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and then you hide the error? Fail doesn't mean no matches, it means that the pattern was invalid, but you hide this.

Copy link
Contributor Author

@tuarrep tuarrep May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok get it! What's the recommended handling in this case? Removing the condition? Errors seem to be ignored for most methods
eg:

preg_match_all($pattern, $this->value, $matches);

@tuarrep
Copy link
Contributor Author

tuarrep commented May 7, 2020

I think you will need to first add this method to the Str class.

As it was similar to explode I've though it'll be sufficient to add this only to Stringable. But I can

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