Re: [PHP-DEV] Proposal for a new basic function: str_contains

This is only part of a thread. view whole thread
  108634
February 17, 2020 07:18 guilliam.xavier@gmail.com (Guilliam Xavier)
On Fri, Feb 14, 2020 at 11:14 AM G. P. B. banyard@gmail.com> wrote:
> > Moreover, checking for a substring to start/end a string seems > to be > fitting for the current strpos functions.
Maybe in terms of semantics (`0 === strpos($haystack, $needle)`), but suboptimal in terms of performance, especially when $haystack is a *very long* string which *doesn't* contain $needle, strpos() will vainly search along the whole string, while a specialized function would stop as soon as possible (which is also the case of existing strncmp() but you need to write `0 === strncmp($haystack, $needle, strlen($needle))`, arguably not really the cleanest code...). For "contains" you have to search along the whole string anyway, so `str_contains()` is "just" `false !== strpos()` but cleaner. To be clear, I'm not against the current proposal (rather for actually) [I just would want `str_{starts,ends}_with` even more (without case-insensitive nor multibyte variants)] -- Guilliam Xavier On Fri, Feb 14, 2020 at 11:14 AM G. P. B. banyard@gmail.com> wrote:
> > On Fri, 14 Feb 2020 at 10:58, Aegir Leet <aegir@aegir.sexy> wrote: > > > I generally like the idea, but it seems many (most?) real-world > > implementations actually use mb_strpos() !== false by default. > > > > > > https://github.com/danielstjules/Stringy/blob/df24ab62d2d8213bbbe88cc36fc35a4503b4bd7e/src/Stringy.php#L206-L215 > > > > https://github.com/illuminate/support/blob/6eff6cff19f7ad5540b9a61a9fb3612ca8218c19/Str.php#L157-L166 > > > > So there should definitely be an mb_str_contains in ext/mbstring in > > addition to the regular str_contains proposed here. > > > > The biggest reason to have an mb_* variant if for when comparing with case > insensitivity. > The only other reason is if you need to check a string which is in a > different encoding, > which is, I'm assuming, is a quasi non-existent problem as everything > things is UTF-8 > nowadays. > > The reason why I personally voted no on the previous RFC was that I don't > see the > value of having functions checking if a string starts/ends with a sequence > but not a > general one. Moreover, checking for a substring to start/end a string seems > to be > fitting for the current strpos functions. > > This function on it's own is way more reasonable and useful to add IMHO > > Best regards > > George P. Banyard
-- Guilliam Xavier
  108639
February 17, 2020 09:02 philipp.tanlak@gmail.com (Philipp Tanlak)
Now that we've talked about the pros and cons of case-insensitivity and
multibyte variants, I'm still unsure what your opinions on those are.

* Should we include a case-insensitive variant (str_icontains) ?
* Should we include multibyte variants (mb_str_icontains) ?

Slightly off-topic:
Also, since this is my first time I'm trying to contribute: How can we
proceed to write an RFC?
I've read in the howto, that I need to earn RFC karma in order to create a
new RFC page. How can I request that?
My wiki.php.net username is: philippta and my email is
philipp.tanlak@gmail.com

Thanks for your help :)
  108641
February 17, 2020 09:52 nikita.ppv@gmail.com (Nikita Popov)
On Mon, Feb 17, 2020 at 10:03 AM Philipp Tanlak tanlak@gmail.com>
wrote:

> Now that we've talked about the pros and cons of case-insensitivity and > multibyte variants, I'm still unsure what your opinions on those are. > > * Should we include a case-insensitive variant (str_icontains) ? > * Should we include multibyte variants (mb_str_icontains) ? >
Especially considering how past proposals in this general area went, I'd suggest to start small (just str_contains), and then go from there. (Personally I'd like to have the trifecta of str_contains, str_starts_with and str_ends_with in one go, but given that a proposal for the latter two recently failed... though the main contention there seems to be the case-insensitive part, not the functions themselves.) Slightly off-topic:
> Also, since this is my first time I'm trying to contribute: How can we > proceed to write an RFC? > I've read in the howto, that I need to earn RFC karma in order to create a > new RFC page. How can I request that? > My wiki.php.net username is: philippta and my email is > philipp.tanlak@gmail.com
I've granted you RFC karma on the wiki, so you should be able to add a new page under wiki.php.net/rfcs now. Regards, Nikita
  108643
February 17, 2020 11:38 philipp.tanlak@gmail.com (Philipp Tanlak)
Am Mo., 17. Feb. 2020 um 10:53 Uhr schrieb Nikita Popov <
nikita.ppv@gmail.com>:

> On Mon, Feb 17, 2020 at 10:03 AM Philipp Tanlak tanlak@gmail.com> > wrote: > >> Now that we've talked about the pros and cons of case-insensitivity and >> multibyte variants, I'm still unsure what your opinions on those are. >> >> * Should we include a case-insensitive variant (str_icontains) ? >> * Should we include multibyte variants (mb_str_icontains) ? >> > > Especially considering how past proposals in this general area went, I'd > suggest to start small (just str_contains), and then go from there. > (Personally I'd like to have the trifecta of str_contains, str_starts_with > and str_ends_with in one go, but given that a proposal for the latter two > recently failed... though the main contention there seems to be the > case-insensitive part, not the functions themselves.) > > Slightly off-topic: >> Also, since this is my first time I'm trying to contribute: How can we >> proceed to write an RFC? >> I've read in the howto, that I need to earn RFC karma in order to create a >> new RFC page. How can I request that? >> My wiki.php.net username is: philippta and my email is >> philipp.tanlak@gmail.com > > > I've granted you RFC karma on the wiki, so you should be able to add a new > page under wiki.php.net/rfcs now. > > Regards, > Nikita >
Thanks for the karma! An RFC has been created: https://wiki.php.net/rfc/str_contains Kind Regards, Philipp
  108645
February 17, 2020 11:48 benjamin.morel@gmail.com (Benjamin Morel)
> > Thanks for the karma! An RFC has been created: > https://wiki.php.net/rfc/str_contains
Something that's missing from the RFC is the behaviour when $needle is an empty string: str_contains('abc', ''); str_contains('', ''); Will these always return false? — Benjamin
  108646
February 17, 2020 11:55 nikita.ppv@gmail.com (Nikita Popov)
On Mon, Feb 17, 2020 at 12:49 PM Benjamin Morel morel@gmail.com>
wrote:

> Thanks for the karma! An RFC has been created: >> https://wiki.php.net/rfc/str_contains > > > > Something that's missing from the RFC is the behaviour when $needle is an > empty string: > > str_contains('abc', ''); > str_contains('', ''); > > Will these always return false? >
As of PHP 8, behavior of '' in string search functions is well defined, and we consider '' to occur at every position in the string, including one past the end. As such, both of these will (or at least should) return true. The empty string is contained in every string. Regards, Nikita
  108647
February 17, 2020 12:08 philipp.tanlak@gmail.com (Philipp Tanlak)
Am Mo., 17. Feb. 2020 um 12:56 Uhr schrieb Nikita Popov <
nikita.ppv@gmail.com>:

> On Mon, Feb 17, 2020 at 12:49 PM Benjamin Morel morel@gmail.com> > wrote: > >> Thanks for the karma! An RFC has been created: >>> https://wiki.php.net/rfc/str_contains >> >> >> >> Something that's missing from the RFC is the behaviour when $needle is an >> empty string: >> >> str_contains('abc', ''); >> str_contains('', ''); >> >> Will these always return false? >> > > As of PHP 8, behavior of '' in string search functions is well defined, > and we consider '' to occur at every position in the string, including one > past the end. As such, both of these will (or at least should) return true. > The empty string is contained in every string. > > Regards, > Nikita >
Thanks for the hint Benjamin. I've cited Nikita and added that to the RFC for clarification.