Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108577 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 47361 invoked from network); 14 Feb 2020 16:00:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Feb 2020 16:00:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 64ED81804F8 for ; Fri, 14 Feb 2020 06:15:05 -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-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) (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 06:15:04 -0800 (PST) Received: by mail-io1-f54.google.com with SMTP id z16so10671084iod.11 for ; Fri, 14 Feb 2020 06:15:04 -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=f7l0quvj6xNfJqbjBGssSpy+rscZBbwDzbfRCU/lVS8=; b=giV1oLJFUiwPDdvvJ3xbMcj82KZR5HqxT5S3I2L465LXgYsznLXD8PhhgxvErSk2hK MLZek7y+dsHVfnPnTtMa8RtJyD4WD5z7YuBk29xy+Xtwyxaik8wXajPG1SeLnL+rIq2E q+5k+3bUm7He9fxhmv/a1G0ANbiIWbPxGRlBPw+sSZITjcOnr1k+SobxCzx5tUuJ6+2V TkufAxIVW8B8Xa0vj18Epgn8oowytiHq6dDVUyOGSELtvyRq1mMwo/Gn8bRq7hWRnVB/ DeDEzWRsjzj1dYCo/bmp8hSid/3vjf9Qa6+Fii3W8ZciB4iznJcvNPGPwjks1eJUSXWL CMNg== 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=f7l0quvj6xNfJqbjBGssSpy+rscZBbwDzbfRCU/lVS8=; b=GN1+trMWcj7Y6yO1z7CpO7TcJa8Jewf8+N/AlF9uuoKswfwKJzrgg0Yn1vPkRndnhH 6ELYJsXG/cQ9q7US782bonUbKv0ftSB5TFYvpBkAuzXcnF70fxedq3jb++hH+sv+7Ci+ cFskfiCz8Ruw9VijzmmdHyKFThnVqYysltvIyWMtFyaXi1M6vYHiO78VDjKiLUymMcP+ TeVZk6WFA3b3ySGf4i2eQMBE1hFoq8klHYZmsTQXIuB2hOS1cCC3InNiAANdh61VPjIA 9OP68UNoM7XvNwvqjixpCilXejtvLQbdDSmScMCUUn98RLiRnfDRUhcpeYiJSZ8z8X3Z 1yLw== X-Gm-Message-State: APjAAAUrjf5LPm/hgXqM9VCwpcqVzhyJY8zzewh37oSkmGrTaYPwzaeG 98yTgWBqMLxSzS8tXYiEgsRG0lFde8ZsZI3qPMw= X-Google-Smtp-Source: APXvYqwuDaEcAKWkvWGHwT2HBycAxBf3nFtReCAS8CgR3T5M1Y0fpu/G/vQZBSvL70LIYoMLXyBij0cOoxFPiYayiuE= X-Received: by 2002:a5e:a616:: with SMTP id q22mr2392814ioi.250.1581689703646; Fri, 14 Feb 2020 06:15:03 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 14 Feb 2020 15:14:27 +0100 Message-ID: To: Nikita Popov Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000ee5078059e89d4b6" Subject: Re: [PHP-DEV] Proposal for a new basic function: str_contains From: philipp.tanlak@gmail.com (Philipp Tanlak) --000000000000ee5078059e89d4b6 Content-Type: text/plain; charset="UTF-8" Am Fr., 14. Feb. 2020 um 12:54 Uhr schrieb Nikita Popov < nikita.ppv@gmail.com>: > 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 > I like to elaborate on Nikitas response: I don't think a mb_str_contains is necessary, because the proposed function does not behave differently, if the input strings are multibyte strings. When searched for a multibyte string in another multibyte string, the return value would consistently be true/false. The position/offset at which the multibyte string was found is not relevant. The reason for the existence of a strpos/mb_strpos is the fact, that the returned position/offset varies depending on weather or not the string is a multibyte string or not. The only possible valid variants concerning multibyte and incasesensitivity I see are: * str_contains: works as expected with multibyte and non multibyte strings. * mb_str_icontains: is the only valid option to do a incasesensitive search for multibyte strings. Unneeded variants I see are: * mb_str_contains: does not behave differently when compared to str_contains, as mentioned above. * str_icontains: is a possible option but could be error prone for when used with multibyte strings like UTF-8, as it is de facto the standard nowadays. I'm certain there would be confusion among php developers when the newly proposed functions are only str_contains and mb_str_icontains. Patrick ALLAERT: Yes, it does have one: people having already defined a str_contains() function in the global scope will have a PHP Fatal error: Cannot redeclare str_contains() You are absolutely correct with this. Although functions added by frameworks to the global scope are usually guarded by: if (!function_exists('str_contains')) {} --000000000000ee5078059e89d4b6--