Re: strrchr with needle with multiple characters
| From: | David CARLIER | Date: | Mon, 02 Sep 2024 11:36:12 +0000 |
| Subject: | Re: strrchr with needle with multiple characters | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125384@lists.php.net to get a copy of this message | ||
Hi,
On Mon, 2 Sept 2024 at 12:20, Vincent Langlet <misterdeviling@gmail.com>
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()
> <https://www.php.net/manual/en/function.strstr.php>.
>
> 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.