Req #74449 [Dup]: Check string begins with function: str_begins()
Edit report at https://bugs.php.net/bug.php?id=74449&edit=1
ID: 74449
Updated by: requinix@php.net
Reported by: stanislav dot eckert at vizson dot de
Summary: Check string begins with function: str_begins()
Status: Duplicate
Type: Feature/Change Request
Package: Strings related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
As is. Duplicate doesn't mean open or closed but that there is another bug/request already
being used to track the issue.
For the sake of automatic linkage: request #50434
Previous Comments:
------------------------------------------------------------------------
[2017-04-16 10:36:28] stanislav dot eckert at vizson dot de
strncmp() is quite good too, but it still requires manually setting a length value and it does not
seem to accept negative offsets (for str_end() like functionality). I understand, that this is not a
truly mandatory function - it just would be nice to have it. Low priority.
Your link references are perfect! I didn't see that, thank you. Should I close the report now
or just leave it as is?
------------------------------------------------------------------------
[2017-04-16 06:34:13] requinix@php.net
See https://wiki.php.net/rfc/add_str_begin_and_end_functions
and the commentary in the PR https://github.com/php/php-src/pull/2049
By the way, have you seen http://php.net/manual/en/function.strncmp.php
?
------------------------------------------------------------------------
[2017-04-15 17:41:38] stanislav dot eckert at vizson dot de
Description:
------------
A quite often used code is to check if a string / binary string begins with a certain substring.
Using substr() requires setting 2nd param always to 0 and 3rd param to length, which is the often
set hardcoded or with strlen() / mb_strlen(). Example:
substr($str, 0, 7) == "http://"
// or
substr($str, 0, strlen("http://")) == "http://"
This can lead to errors, when user sets wrong length, mixes substr() & strlen() with their
multibyte versions, etc. A shorter version could be done with strpos() and checking for position 0,
but strpos() searches the entire string. RegEx has performance implications.
A simple function would result in a cleaner and a more robust and readable code. Example:
str_begins($str, $substr) // => true / false
Its performance can also be optimized quite good, I think (compared to substr()).
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74449&edit=1
Thread (4 messages)