Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125384 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 AAB551A00BD for ; Mon, 2 Sep 2024 11:36:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725277107; bh=TPFv/5TJnquNKmsxvQhrhA3W03c0filDarFJLcuH92M=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=YmNW2sFvLJFsmAVU1x/LF2BDx3MDfwsI6jeF2+tJ+FKO2Atl4AwquppDxrV1gdR8t 0EoyTJBA/ayUZ1SksvjNLzqUEo3gqSdj9Eq/eV+RePTASj6+ksN4Gynvo7mJpVg0jq ALGFol2IoIPqyWHsF2dqEqiu1iF5jX+zYPzxxdak75xdOLCzoLD/vwImmAeb+jZYLJ zhSPAK4IF/UPQaB/mrC2TFYbWTqaK0jQ9anuoUODvIch0qrfIfiGivUxonbwdMIneO oN+wxUXrCR2QbflqrF1u87rEyjtT8UIrTC74K1Kl7gCAmXotEs1S6KxvhNjkmwbNRO WNGNvlCZCiDVw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8110718006A for ; Mon, 2 Sep 2024 11:38:26 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,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 mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 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 11:38:23 +0000 (UTC) Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-6c3551ce5c9so17522466d6.0 for ; Mon, 02 Sep 2024 04:36:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725276986; x=1725881786; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TPFv/5TJnquNKmsxvQhrhA3W03c0filDarFJLcuH92M=; b=X9R4V0QhkcGKqHi/gd5T90dcBA7NA+nFtNZwv4EheQJQigmrWeb8QeARVcEknArKm6 CkmC/q6vCD2cRus4kLJRx78C6J+57wyUdm5dS9gK1qAnRGZzloEVYD1cBetsWc5vf1JL z3WEV9ILOoTzA3/Wj4xmel8d/hr8R5Kt/8q6439u8KAGkzrqfl5XGnUkg3abuY5Gt+Ek gKb9KWLzLJR+/HOjWiqFdZC6pMskNCKsCKIdDfCCp/SMaPRp6ODsNf4kJvorPfqo1dl3 k4IJzvrgmlPbzyCNTTtTdZ7Kye8Hz2Wq6lvfvMquIbmPS/HQTNTfldbAEcATKL+v1q3H diCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725276986; x=1725881786; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TPFv/5TJnquNKmsxvQhrhA3W03c0filDarFJLcuH92M=; b=VRaDqB4TQcM4rjJIlkMm9wSEWcZE2BHP2zKMsTldAaT0EAgYS7pLgwYn367mJFfx2F QRNuyemh0oAKcEalhkkOkAD4suQ/uGD1p2rgZIHBdTiHp5EFPkh2/SmSH2TP4EU3Fua+ GSRoeXwaUtlnKspIy5MgvJa1CXLhNZEQ1+IrNJ0sSKRBg4gN+rMnszWyOKJSGGvqHc9z 9En4AJ9OQehBM9y5QrDmGlze3DpuTjh6G+2XRCBfn6jmm+YDT5g26xp/cT7WkVNK4sT2 4b31iuT6RPZjtH63Yr2OiKUc57tfAy1jkHHenemB2wdxrBJylnsEvn/c2J7c3si8FWtr gfhg== X-Gm-Message-State: AOJu0YxnjrZdx16okTGKiQvWT0a5rWYWl5m+leV5/7zBzET2ne2AAoq/ wOFF0MUcERgioPvX9wRJkM1TNSY5ZGRIbFJ0QpIJOjqEFsyezoKrf7+ikHi+HOt8Yrhsom1yFaf JW3qA9f9VBlfY2nvECATimDc2pXUbk05e X-Google-Smtp-Source: AGHT+IG9itxHY/wdjLu3hsKSAynkXFRJzyG7UwNy8Yw1a7qct/IWx3QHBeRq5RZna2DhrKQ0CFSEfoD+KcwiFS51IW8= X-Received: by 2002:a05:6214:4382:b0:6c3:5c75:d2b1 with SMTP id 6a1803df08f44-6c35c75da04mr90909316d6.5.1725276985780; Mon, 02 Sep 2024 04:36:25 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 2 Sep 2024 12:36:12 +0100 Message-ID: Subject: Re: [PHP-DEV] strrchr with needle with multiple characters To: Vincent Langlet Cc: PHP Internals List Content-Type: multipart/alternative; boundary="000000000000e0710006212159fd" From: devnexen@gmail.com (David CARLIER) --000000000000e0710006212159fd Content-Type: text/plain; charset="UTF-8" Hi, On Mon, 2 Sept 2024 at 12:20, 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. > This 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 first > 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 > have no knowledge at all in C and in the existing PHP code) ? > It is a bit ambiguous, I'll give you that, albeit you can pass a string to str(r)chr only the first character is used, these are literally similar to their C counterparts https://www.tutorialspoint.com/c_standard_library/c_function_strchr.htm https://www.tutorialspoint.com/c_standard_library/c_function_strrchr.htm see the second argument handles one character at a time. Cheers. --000000000000e0710006212159fd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

<= div dir=3D"ltr" class=3D"gmail_attr">On Mon, 2 Sept 2024 at 12:20, Vincent = Langlet <misterdeviling@gmai= l.com> wrote:
I was today old when I discovered the big difference = between strchr and strrchr in the way they handle needle with multiple char= acters.

It's explained in the doc=C2=A0https://www.php.net= /manual/en/function.strrchr.php
-=C2=A0If=C2=A0needle=C2=A0= contains more than one character, only the first is used. This behavior is = different from that of=C2=A0= strstr().

You can see an example=C2=A0https://3v4l.org/7j5ab

I feel like this behavi= or has no benefit (if I want to look for the first character, I can pass `s= ubstr($needle, 0, 1)` instead) and is just error-prone.

Is there a r= eason for such behavior ? Would it be easy to change it (I have no knowledg= e at all in C and in the existing PHP code) ?

It is a bit ambiguous, I'll give you that, albeit you can pass= a string to str(r)chr only the first character is used, these are literall= y similar to their C counterparts

<= div>https://www.tutorialspoint.com/c_standard_library/c_function_= strrchr.htm

see the second argument handle= s one character at a time.

Cheers.
--000000000000e0710006212159fd--