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

Create str_contains PHP function #5179

Closed
wants to merge 3 commits into from
Closed

Conversation

philippta
Copy link
Contributor

@philippta philippta commented Feb 13, 2020

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 or strstr.

This type of function exists for example for arrays (in_array), while there is still array_search (equivalent to strpos) available.

I would like to propose the new function str_contains:

str_contains ( string $haystack , string $needle ) : bool

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:

<?php

str_contains("abc", "a"); // true
str_contains("abc", "d"); // false

@nikic nikic added the RFC label Feb 13, 2020
Copy link
Member

@nikic nikic left a 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).

ext/standard/basic_functions_arginfo.h Show resolved Hide resolved
@philippta
Copy link
Contributor Author

I'm in favor, but this will need an RFC (https://wiki.php.net/rfc/howto).

This sounds great. I will go ahead and create an RFC for this.

@jrbasso
Copy link
Contributor

jrbasso commented Feb 16, 2020

To avoid another PR/RFC, what do you think in including a str_icontains function too? Many of other str functions has the i version.

@Girgias
Copy link
Member

Girgias commented Feb 16, 2020

To avoid another PR/RFC, what do you think in including a str_icontains function too? Many of other str functions has the i version.

See https://externals.io/message/108562 as to why it doesn't make a lot of sense to include a case insensitive version.

@philippta
Copy link
Contributor Author

An RFC has been created: https://wiki.php.net/rfc/str_contains

@laruence
Copy link
Member

laruence commented Feb 21, 2020

I am not sure what is the difference between this and if (strpos === false).

@KennedyTedesco
Copy link
Contributor

To me, as a user, it is purely about UX. Having a str_contains it's better than using strpos.

@lcobucci
Copy link

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
Copy link
Contributor Author

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.

@Gemorroj
Copy link
Contributor

Gemorroj commented Mar 14, 2020

what about multibyte support?

// UTF-8
str_contains("тест", "т"); // ???

@Girgias
Copy link
Member

Girgias commented Mar 14, 2020

what about multibyte support?

// UTF-8
str_contains("тест", "т"); // ???

That would return true

@Gemorroj
Copy link
Contributor

@Girgias maybe add third option for case-insensitive cases? str_contains("abc", "A");

str_contains ( string $haystack , string $needle, bool $caseInsensitive = false ) : bool

or make new function str_icontains for case-insensitive cases

@Girgias
Copy link
Member

Girgias commented Mar 14, 2020

@Girgias maybe add third option for case-insensitive cases? str_contains("abc", "A");

str_contains ( string $haystack , string $needle, bool $caseInsensitive = false ) : bool

or make new function str_icontains for case-insensitive cases

... Have you read this PR thread and or the RFC? I've linked reasons as to why it doesn't make sense: #5179 (comment)
The RFC also says why it is not in the proposal: #5179 (comment)

1 similar comment
@Girgias
Copy link
Member

Girgias commented Mar 14, 2020

@Girgias maybe add third option for case-insensitive cases? str_contains("abc", "A");

str_contains ( string $haystack , string $needle, bool $caseInsensitive = false ) : bool

or make new function str_icontains for case-insensitive cases

... Have you read this PR thread and or the RFC? I've linked reasons as to why it doesn't make sense: #5179 (comment)
The RFC also says why it is not in the proposal: #5179 (comment)

@Gemorroj
Copy link
Contributor

Have you read this PR thread and or the RFC?

No. But now yes. There are answers to all my questions. Thanks.
It would be good to specify a link to the RFC in the first post, so that there are no such misunderstandings.

@philippta
Copy link
Contributor Author

The RFC has been accepted and the voting phase has been closed.
It is now ready to be merged.

@php-pulls php-pulls closed this in 1668ad7 Mar 16, 2020
@carusogabriel carusogabriel added this to the PHP 8.0 milestone May 29, 2020
@scottchiefbaker
Copy link

Are there official polyfills for the new this and the other new string functions?

@Gemorroj
Copy link
Contributor

@scottchiefbaker
Copy link

scottchiefbaker commented May 30, 2020

@Gemorroj perfect, thank you

@leernd007
Copy link

I am not sure what is the difference between this and if (strpos === false).

Repurposing strpos and strstr for this use-case has a few down sides. Either, they are:

not very intuitive for a reader
easy to get wrong (especially with the !== comparison)
or hard to remember for new PHP developers.

https://wiki.php.net/rfc/str_contains

@sfaut
Copy link

sfaut commented Jul 13, 2020

I am not sure what is the difference between this and if (strpos === false).

Empty string management differs. strpos('foo', '') emits a warning and returns false.

When I search something I don't want always to have true returned in some cases. I think it is bug-compliant.

franzholz added a commit to franzholz/typo3db_legacy that referenced this pull request Jan 15, 2022
 * 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)
@dani7115
Copy link

dani7115 commented May 7, 2022

I always wanted this, Since the day i started programming in PHP :) . Thanks Finally

Davidmattei added a commit to Davidmattei/elasticms that referenced this pull request Dec 1, 2022
georgfranz added a commit to franz-agency/zetacomponents-Authentication that referenced this pull request Dec 11, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet