-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Create str_contains PHP function #5179
Conversation
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.
I'm in favor, but this will need an RFC (https://wiki.php.net/rfc/howto).
163c576
to
0de0e3d
Compare
0de0e3d
to
5994551
Compare
This sounds great. I will go ahead and create an RFC for this. |
To avoid another PR/RFC, what do you think in including a |
See https://externals.io/message/108562 as to why it doesn't make a lot of sense to include a case insensitive version. |
An RFC has been created: https://wiki.php.net/rfc/str_contains |
I am not sure what is the difference between this and if (strpos === false). |
To me, as a user, it is purely about UX. Having a |
Could you please add a test covering the examples you have in the RFC? That would be quite good to ensure that things behave properly throughout time 👍 |
Tests have been added. |
what about multibyte support? // UTF-8
str_contains("тест", "т"); // ???
|
That would return |
@Girgias maybe add third option for case-insensitive cases?
or make new function |
... Have you read this PR thread and or the RFC? I've linked reasons as to why it doesn't make sense: #5179 (comment) |
1 similar comment
... Have you read this PR thread and or the RFC? I've linked reasons as to why it doesn't make sense: #5179 (comment) |
No. But now yes. There are answers to all my questions. Thanks. |
The RFC has been accepted and the voting phase has been closed. |
Are there official polyfills for the new this and the other new string functions? |
@Gemorroj perfect, thank you |
Repurposing strpos and strstr for this use-case has a few down sides. Either, they are: not very intuitive for a reader |
Empty string management differs. When I search something I don't want always to have true returned in some cases. I think it is bug-compliant. |
* RemoveUnusedVariableInCatchRector (https://wiki.php.net/rfc/non-capturing_catches) * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * StrContainsRector (https://externals.io/message/108562 php/php-src#5179)
I always wanted this, Since the day i started programming in PHP :) . Thanks Finally |
Applied rules: * TokenGetAllToObjectRector (https://wiki.php.net/rfc/token_as_object) * StrEndsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrContainsRector (https://externals.io/message/108562 php/php-src#5179)
Applied rules: * StrEndsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrContainsRector (https://externals.io/message/108562 php/php-src#5179)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotion php/php-src#5291) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrContainsRector (https://externals.io/message/108562 php/php-src#5179) * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * LongArrayToShortArrayRector * TernaryToNullCoalescingRector * PublicConstantVisibilityRector (https://wiki.php.net/rfc/class_const_visibility) * ListToArrayDestructRector (https://wiki.php.net/rfc/short_list_syntax https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring) * MixedTypeRector * StrContainsRector (https://externals.io/message/108562 php/php-src#5179) * ChangeSwitchToMatchRector (https://wiki.php.net/rfc/match_expression_v2) * FinalizePublicClassConstantRector (https://php.watch/versions/8.1/final-class-const) * NullToStrictStringFuncCallArgRector * TypedPropertyFromAssignsRector
There is currently no function to check if a string contains another string. The only way to achive this behavior is to use other functions like
strpos
orstrstr
.This type of function exists for example for arrays (
in_array
), while there is stillarray_search
(equivalent tostrpos
) available.I would like to propose the new function
str_contains
:It takes a $haystack and $needle as a parameter and checks if the $haystack string contains the $needle string. It returns a boolean value (
true
/false
) to indicate weather the $needle is found.Example: