[10.x] Add isMatch method to Str and Stringable helpers #46303
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds isMatch method for both
Illuminate\Support\Str
andIlluminate\Support\Stringable
.Additionally, I recently had a need to perform a regex pattern match: I just needed to check that my variable contains the complex regex pattern. While I was trying to find a solution, I found a problem.
Str
does not contain method that accepts complex regex pattern and returns boolean.First of all
Illuminate\Support\Str
andIlluminate\Support\Stringable
already has methods to perform pattern matching. There areis
andmatch
methods:is()
is good for checking string in more human-readable way, but it not supports complex patterns.match()
in the other hand supports complex pattern matching but returns string that has been matched. It forces us to additionally check that result of the method call is not containing an empty string, which is not the same thing as check if a string contains pattern.So I suggest adding an isMatch method that will allow you to check the string for the presence of a pattern in it.
Some examples:
✔️This PR does not contain breaking changes.
✔️This PR only adds new features.