Req #74449 [NEW]: Check string begins with function: str_begins()

From: Date: Sat, 15 Apr 2017 17:41:41 +0000
Subject: Req #74449 [NEW]: Check string begins with function: str_begins()
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-208581@lists.php.net to get a copy of this message
From:             stanislav dot eckert at vizson dot de
Operating system: 
PHP version:      Irrelevant
Package:          Strings related
Bug Type:         Feature/Change Request
Bug description:Check string begins with function: str_begins()

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 bug report at https://bugs.php.net/bug.php?id=74449&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=74449&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=74449&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=74449&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=74449&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=74449&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=74449&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=74449&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=74449&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=74449&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=74449&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=74449&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=74449&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=74449&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74449&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=74449&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=74449&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=74449&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74449&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=74449&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=74449&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#208581) next »