Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108569 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 19530 invoked from network); 14 Feb 2020 13:39:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Feb 2020 13:39:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D76CD1804F8 for ; Fri, 14 Feb 2020 03:54:16 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 14 Feb 2020 03:54:16 -0800 (PST) Received: by mail-lj1-f174.google.com with SMTP id x7so10426784ljc.1 for ; Fri, 14 Feb 2020 03:54:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wVSCB63HHzajLQb1CvqdlwZOib59hJBIAIlGaJlEvZ4=; b=ofWPPUPj6ZZWYnwE9JKe4qCnezvPCOuWiyUeLdHm1pvXrzVv10o11RsSpA4JB3XB3J /tlVFs6ZCZPm6sVaxp9ltFbbhMquEdSL1R18b7mr94vHglnvaaBZWzGuRMTvjqBM44rm MYSbWO96XHBPF4VwjiJoNZuL2YMkWRVJXFm9VyKOpZxMZxHx8bUeQwvqVBI34YKKj5W6 3uc1kRHje/wdW9p/9Uf8iG6nfXsgOxQJ9BckmKLseujh40zjmTNyjjqlncn6nY1wPj42 XtXX6dk1R+SBLts1wzkU5+0jxXfiUQh9frUIU7RCOYGE4+ZZ3d2evD3LsFtIZbY81NXP N1rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wVSCB63HHzajLQb1CvqdlwZOib59hJBIAIlGaJlEvZ4=; b=Cd7KnaA6i5ZKps3Oy2QqJklLTEL0t9AUf38Q2mO8NWDFZ1qWhcsNnBP98u1cBPmIw8 WEvLbCKwzD6m02Fe+6OlkyVjeXUIo3ID6DrOJtn0dTrFvHqFLh1MCo3uMS5CJWis/HM3 q3fVDiarJB12NjmdsTGz79eepvcQ+v6KL2yHjOhtu9f4H2An+zXTumpC9ytAdUW7tPx7 sV2nmXCZSF8ukAt71laht7BpGkFhXnfrjTJVhs4cp7GnU6xne6BqES3/Gom+PazLFilK 4ItjaJ2obnk3fOENMQtpPr+LfOrv9RfdsVCX4wCFT/yBTcyz8CK3YHg1B/Tc12T42lz4 rfGg== X-Gm-Message-State: APjAAAV5+/QdWDPNJiOg4DIhyXUGHcVdUD8LufD1VyGjtsvW+ApTXXZm Sbeqn6QWqxME5Ckh2CPXemJvu+VMQLLZk5ibXvU= X-Google-Smtp-Source: APXvYqydsEHY6AZFNetWcLc/liy+ntMBH0FfSD4PFM4xPyDD4MWJRPKBzAua7lE7xGxxO208SnDNiO1dNX6HOd0zJpw= X-Received: by 2002:a05:651c:297:: with SMTP id b23mr1915119ljo.260.1581681253097; Fri, 14 Feb 2020 03:54:13 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 14 Feb 2020 12:53:57 +0100 Message-ID: To: Philipp Tanlak Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000003d240d059e87dda4" Subject: Re: [PHP-DEV] Proposal for a new basic function: str_contains From: nikita.ppv@gmail.com (Nikita Popov) --0000000000003d240d059e87dda4 Content-Type: text/plain; charset="UTF-8" On Fri, Feb 14, 2020 at 10:18 AM Philipp Tanlak wrote: > Hello PHP Devs, > > I would like to propose the new basic function: str_contains. > > The goal of this proposal is to standardize on a function, to check weather > or not a string is contained in another string, which has a very common > use-case in almost every PHP project. > PHP Frameworks like Laravel create helper functions for this behavior > because it is so ubiquitous. > > There are currently a couple of approaches to create such a behavior, most > commonly: > strpos($haystack, $needle) !== false; > strstr($haystack, $needle) !== false; > preg_match('/' . $needle . '/', $haystack) != 0; > > All of these functions serve the same purpose but are either not intuitive, > easy to get wrong (especially with the !== comparison) or hard to remember > for new PHP developers. > > The proposed signature for this function follows the conventions of other > signatures of string functions and should look like this: > > str_contains(string $haystack, string $needle): bool > > This function is very easy to implement, has no side effects or backward > compatibility issues. > I've implemented this feature and created a pull request on GitHub ( Link: > https://github.com/php/php-src/pull/5179 ). > > To get this function into the PHP core, I will open up an RFC for this. > But first, I would like to get your opinions and consensus on this > proposal. > > What are your opinions on this proposal? > Sounds good to me. This operation is needed often enough that it deserves a dedicated function. I'd recommend leaving the proposal at only str_contains(), in particular: * Do not propose a case-insensitive variant. I believe this is really the point on which the last str_starts_with/str_ends_with proposal failed. * Do not propose mb_str_contains(). Especially as no offsets are involved, there is no reason to have this function. (For UTF-8, the behavior would be exactly equivalent to str_contains.) Regards, Nikita --0000000000003d240d059e87dda4--