php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78821 strbrk should accept a $length parameter
Submitted: 2019-11-16 00:00 UTC Modified: 2021-01-13 11:30 UTC
From: jerry at jmweb dot net Assigned:
Status: Suspended Package: Strings related
PHP Version: Irrelevant OS: n/a
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jerry at jmweb dot net
New email:
PHP Version: OS:

 

 [2019-11-16 00:00 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-16 00:10 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2019-11-16 00:10 UTC] requinix@php.net
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
 [2019-11-16 11:56 UTC] jerry at jmweb dot net
-Status: Feedback +Status: Open
 [2019-11-16 11:56 UTC] jerry at jmweb dot net
My objective was to avoid introducing a new core function by enhancing an existing function in a BC way. strpbrk() seemed to fit the bill neatly. The description of $length was borrowed from substr() which, if positive, indicates the maximum length of the returned string. If strpbrk() is enhanced as recommended, then I would implement as follows:

if( strlen( strpbrk( $string, $allowed, 1 ) ) ){ /* .. do something */}

Here is an example of a userland implementation (https://3v4l.org/cp19M) but the obvious benefit of implementing at the core level is to avoid returning the remaining segment of the string once a character is found.

Thank you for pointing out strcspn(). Based on your example, the inline implementation is similar, albeit longer, to the strpbrk() variant shown above:

if( strcspn( $string, $allowed ) < strlen( $string ){ /* .. do something */}

To cover edge cases, I wrapped it up and added it to my library (https://3v4l.org/0C6sQ).

Thanks.
 [2021-01-13 11:30 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-01-13 11:30 UTC] cmb@php.net
This needs discussion, for which this bug tracker is not suitable.
Feel free to pursue the RFC process[1].  For the time being, I'm
suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC