Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125387 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 40CF11A00BD for ; Mon, 2 Sep 2024 13:24:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725283600; bh=5sUBviTGaljuptFiCmovLzEv6op4KiI5bx2DKB5pvGk=; h=Date:Subject:To:References:From:In-Reply-To:From; b=di+L6StWvAsu3f8Cz02G5MRgNr0RtmaSSYT0SGgMFQmJoKelvZovsbsO7A93dPh9H ICffNYWfqP1k5e5e/LmblBLXH+t2aVJSQyBBg+GqXZkvPYK7VuauZCuknUDqGN7eqv D3YLG/hMusL7EY96vBsC6dxCIO+cJQ2IvHETKNtTsxIBwFsRwqPFuYmpDpIWPMxnw1 fzVnFBTY4XZR25fpCkz7BtoIaYY6ufV7xm7R5OQdDtpQmty7nYV/DQED1gg2QRBB2a EuzY9vbMQy4b5kijFODa9cm0QofLDGHOgbCgRTbTIyD+XpnFqb0HlcmeKKHgayg5tb PG30OeIdwWU4Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1C45218006A for ; Mon, 2 Sep 2024 13:26:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 2 Sep 2024 13:26:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1725283480; x=1725888280; i=cmbecker69@gmx.de; bh=x9uQ5bw45Wb4iZbIKRjmJiqwoor4NAcYgL++spB9+Vg=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:To: References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding:cc:content-transfer-encoding: content-type:date:from:message-id:mime-version:reply-to:subject: to; b=qyjXhiJcgGX7x1FuL2y0OS7XTz0KWr65bv+ds+SQpZU+4rjEoCX7HVd2ybkspdFk OVfs3oE5uQtDHRWjwmF91Dy5R+VBTIbwhePrHWsKg5qelXPtKBybjdhVGF4mnqq4N hfJy1I69EphnRCncMTXTdEjbq4j17YZYVDyjcOfJjlV2AjO2yVsLQXVhVDxNfFCmB lie3ApVjSdiTfrvhupeO56EYE8PZqMPzKW3PD2y9EfvXtWHeyYpsd+v51SjZBZDdd 6yXc4oTJPLTl5C61/tbPx9gQ1HaXH+RdZMUEH1+hohuqIaqaxKDQkMMztm+MIEHXO eVcIzcasJ0GWlrXHTg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.2.130] ([79.251.205.37]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1Mof9F-1sI2gz1GZ7-00nNDc; Mon, 02 Sep 2024 15:24:40 +0200 Message-ID: Date: Mon, 2 Sep 2024 15:24:40 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] strrchr with needle with multiple characters Content-Language: de-DE To: Vincent Langlet , PHP Internals List References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:5NfZlZ0hn8I21AoAy/yllf52txMwYxCiKZQMRp/WSHbOruphVkh qVdZTtBSFRUa57hZGH2thAv1BoVgKLUc6DjuAMMkWFcyzcx2+Vd8Sjel3EE7QWPlnAfLq1U t8IMrWaZhI99YVcB3Gvs6eJwT3Ovj3uQ4wtq+rJIsqJYYfCWXRLqrGAzabYlRkIXC2/gAh3 YMm7hSzaioo5a9h5cxyuw== UI-OutboundReport: notjunk:1;M01:P0:oi2kbo/uzEo=;l5cYCgN1VlQ8Ox1LLVccFqVwHEO GcjsJQJ7NM8hf9+BLg/LkYBobMswhl2WHr9jwGnhmkjO72SkbXFD2PVUyopIefhUpjhoj0YNy ZEOIe7IRVglOpjUC41m8wxOZERUdgkdRE+FNYGh33OoRaRSUHkjOnFFZXOhn2IUR0RxaBSEz3 DXIxmkSldVCHTMJDGEheQCYlAzZe7mUoa8hLD6M/QukSgNT9eYiK3KCo+spH4Ur/4HzumpRCM 4B7fQ3K/PNqqpigcM/AqPeALwCMn+LZeLR8JrSrTrSeD0cpl7RdbmBRh5fmVl8HSDwRB1uO7o tprWAqi1kV5D51nw7xf3vC3nC0gxyBFkRcD3v9XJrcQflrw4OhkuVkB5SOKQNl++SNPYJWAV8 NeLpRSTZyWyxDBI0WkFixUSI1u3AAHYTNIe1CwFGpMJMDhsSiJBn7DOwtDSdD2Mois+tKkfsN wNy49ohbux9RLoHSdfprq/QQFmuEUuinclRK4z65Eh6IJGZqzMIie4K/JT+ypOaymMxY08qCN F648p3HYn0k1s2FlK57zHO//szdecIF5K9yUrgeTebTjAXz5yUjQaFfLgBe80sdT+E79GcK7f LZrYIsInvrvs9EJFvfPqgQT5Q7gRCGf1n0HfgEu3/PcqMHBliK6IWCaKSZnGRNf7QcvknuxZS X99P8WTkGlKUhqxAdED0snvcHaCrpUYEM12TdqZwkjXjdZHulxtYrsnYpaKanW88nfgtvzbnK In1hEGV+fb3kQlZUV0gOh1VmDspasCCHFGGxIlU5BoXx6XJoT+wy2JvrZJJzEBmjuKn5Sl+2c rQT1YuhbHoD8KTkZo1Pm9Pww0oWcgwb3xLMoPd25CNHxQ= From: cmbecker69@gmx.de ("Christoph M. Becker") On 02.09.2024 at 13:17, Vincent Langlet wrote: > I was today old when I discovered the big difference between strchr and > strrchr in the way they handle needle with multiple characters. > > It's explained in the doc https://www.php.net/manual/en/function.strrchr= .php > - If needle contains more than one character, only the first is used. Th= is > behavior is different from that of strstr() > . > > You can see an example https://3v4l.org/7j5ab > > I feel like this behavior has no benefit (if I want to look for the firs= t > character, I can pass `substr($needle, 0, 1)` instead) and is just > error-prone. > > Is there a reason for such behavior ? Would it be easy to change it (I h= ave > no knowledge at all in C and in the existing PHP code) ? strchr() is an alias of strstr(), and I would always use the latter name, because strchr() is indeed confusing. Anyhow, strstr() is very useful in C, but not so much in PHP, since in PHP it will always allocate a new string, even if that is not needed. Now, comparing strstr() and strrchr() is of course still possible, but different behavior is less astonishing. Of course, strrchr() could warn or even throw if $needle has more than one character, but that may unnecessarily break code for a small benefit. Christoph