Req #78821 [Opn->Fbk]: strbrk should accept a $length parameter
| From: | requinix@php.net | Date: | Sat, 16 Nov 2019 00:10:59 +0000 |
| Subject: | Req #78821 [Opn->Fbk]: strbrk should accept a $length parameter | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-223739@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78821&edit=1
ID: 78821
Updated by: requinix@php.net
Reported by: jerry at jmweb dot net
Summary: strbrk should accept a $length parameter
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: Strings related
Operating System: n/a
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I'm not sure I understand. Doesn't a $length imply that strpbrk will only check the first
$length characters of $haystack? How does that help your use case of checking "if a single
character, among a list of characters, exists in a string"?
Instead, have you seen strcspn? https://3v4l.org/G3orE
Previous Comments:
------------------------------------------------------------------------
[2019-11-16 00:00:26] jerry at jmweb dot net
Description:
------------
Sometimes we just need to know if a single character, among a list of characters, exists in a
string. strbrk() is almost what is needed but instead it returns a substring of the original
starting from the first occurrence.
Admittedly, I am not a C person but inspecting the source code indicates that this feature should be
trivial. Here is what I propose the new signature should be:
strpbrk ( string $haystack , string $char_list [, int $length ] ) : string;
If $length is given and is positive, the string returned will contain at most $length characters.
If I follow correctly, "RETURN_STRINGL" ends up calling "zend_string_init" which
accepts a "len" parameter as its second argument. How difficult would it be to add this
feature?
https://github.com/php/php-src/blob/93ba3abe63cd339c52cf5774e948bb35d8e73049/ext/standard/string.c#L6078
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78821&edit=1