Uh oh!
There was an error while loading. Please reload this page.
Create str_contains PHP function - #5179
Conversation
There was a problem hiding this comment.
I'm in favor, but this will need an RFC (https://wiki.php.net/rfc/howto).
Uh oh!
There was an error while loading. Please reload this page.
philippta
commented
Feb 14, 2020
This sounds great. I will go ahead and create an RFC for this. |
jrbasso
commented
Feb 16, 2020
To avoid another PR/RFC, what do you think in including a |
Girgias
commented
Feb 16, 2020
See https://externals.io/message/108562 as to why it doesn't make a lot of sense to include a case insensitive version. |
philippta
commented
Feb 17, 2020
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). |
KennedyTedesco
commented
Feb 21, 2020
To me, as a user, it is purely about UX. Having a |
lcobucci
commented
Mar 3, 2020
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 👍 |
philippta
commented
Mar 13, 2020
Tests have been added. |
what about multibyte support? // UTF-8str_contains("тест", "т"); // ??? |
Girgias
commented
Mar 14, 2020
That would return |
Gemorroj
commented
Mar 14, 2020
@Girgias maybe add third option for case-insensitive cases? or make new function |
Girgias
commented
Mar 14, 2020
... 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
Girgias
commented
Mar 14, 2020
... Have you read this PR thread and or the RFC? I've linked reasons as to why it doesn't make sense: #5179 (comment) |
Gemorroj
commented
Mar 14, 2020
No. But now yes. There are answers to all my questions. Thanks. |
philippta
commented
Mar 16, 2020
The RFC has been accepted and the voting phase has been closed. |
scottchiefbaker
commented
May 30, 2020
Are there official polyfills for the new this and the other new string functions? |
Gemorroj
commented
May 30, 2020
@Gemorroj perfect, thank you |
leernd007
commented
Jun 3, 2020
Repurposing strpos and strstr for this use-case has a few down sides. Either, they are: not very intuitive for a reader |
sfaut
commented
Jul 13, 2020
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_promotionphp/php-src#5291) * StrContainsRector (https://externals.io/message/108562php/php-src#5179)
dani7115
commented
May 7, 2022
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/108562php/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/108562php/php-src#5179)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotionphp/php-src#5291) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrContainsRector (https://externals.io/message/108562php/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_syntaxhttps://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring) * MixedTypeRector * StrContainsRector (https://externals.io/message/108562php/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
strposorstrstr.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: