Re: Introduce str_left/right In 8.1
| From: | Christoph M. Becker | Date: | Wed, 23 Jun 2021 21:28:26 +0000 |
| Subject: | Re: Introduce str_left/right In 8.1 | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-115079@lists.php.net to get a copy of this message | ||
On 23.06.2021 at 21:10, Mike Schinkel wrote:
> Replying to both Sara's and G.P.B.'s emails below:
>
>> On Jun 23, 2021, at 12:48 PM, Sara Golemon <pollita@php.net> wrote:
>>
>> Using some context, I would assume you mean this:
>>
>> function str_left(string $str, int $len): string {
>> return substr($str, 0, $len);
>> }
>>
>> function str_right(string $str, int $len): string {
>> return substr($str, -$len);
>> }
>>
>> If that's the case, then.... why? As you can see, the existing
>> functionality available is trivial to write.
>> <snip>
>> Am I misunderstanding what these proposed functions should do, or am I
>> underestimating the difficulty of typing a zero or negative sign on certain
>> keyboards?
>
> I can't speak for Hamza, but one reason I can see for why — at least for str_right() —
> is that using a negative index might not occur to many people. Hell, I even forgot substr() worked
> that way until you mentioned it.
>
> But, since objective evidence is better than opinion, a non-trivial number of people have
> searched how to get the last 'n' characters from a string in PHP, found the answer, and
> then went to the effort to upvote it on StackOverflow. If the 90-9-1 rule holds, then that's
> about 6200 people who have been confused, searched and found the answer here:
>
>
> https://stackoverflow.com/questions/10542310/how-can-i-get-the-last-7-characters-of-a-php-string
>
> <https://stackoverflow.com/questions/10542310/how-can-i-get-the-last-7-characters-of-a-php-string>
substr() is about bytes, not characters. They all may have upvoted the
wrong answer. The only correct answer has just 17 upvotes.
Christoph