-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.4] Add str_after helper function #19357
[5.4] Add str_after helper function #19357
Conversation
Very useful. He rarely merges helpers that aren't used in the framework itself, but 'fingers-crossed' |
I doubt the helper will get merged for the reason @devcircus said, but I wouldn't doubt the |
@JosephSilber |
I can't decide what I think about returning empty string if the given string isn't present. To me it feels like I would want the whole string returned, since it most cases I would be using this function to trim a given string off the front of the other string. Thoughts? |
If it's a simple "trim this off this", why not use a preg_replace, with a leading ^
|
I agree there are existing options if it's a leading trim, this would just be a more readable version of it maybe. Anyways, probably need @calebporzio to comment with his thoughts behind it. |
I'm good with returning the entire string rather than empty. That makes more sense to me. I think I adjusted the implementation to return the full string on failure. I didn't go the regex route, let me know if that's a problem. |
The docs for strpos
The docs for strstr ...
The docs for substr ...
It kind of makes sense to keep the php tradition of returning false for these kind of string operations. Substr's empty string is a special case when pass $length = 0. If you you pass a $start which is after the length of the input you still get a false back. If you want the original string if the needle is missing, use |
There is room for optimization in this code, and at least an unit test with empty search should be added, because |
return $subject; | ||
} | ||
|
||
$searchEndPos = strpos($subject, $search) + static::length($search); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strpos
is single-byte while the rest of the code is utf8 aware.
Therefore you get wrong results with after('é foobarbaz', 'foo')
|
||
$searchEndPos = strpos($subject, $search) + static::length($search); | ||
|
||
return static::substr($subject, $searchEndPos, static::length($subject)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We grab up to the end, so the third parameter should be omitted.
(as more, here it exceeds the end the subject; it works but is erroneous)
This seems unintuitive if there are multiple instances of the search string. I think I'd expect |
There can be third parameter offset like after($subject, $search, $offset) to skip number of character / occurrence. |
I often come across cases where I need to retrieve everything after a piece of a string. Manually
preg_match
orstrpos
/substr
is verbose IMO.Here is the usage:
I've been using my own personal helper for this for some time now and have found it extremely handy, hoping you find the same!
Thanks for the endless amounts of hard work!