Req #54387 [Opn->Sus]: Add str_slice() function, simple & logical string slicing function

From: Date: Sun, 08 Apr 2018 15:49:07 +0000
Subject: Req #54387 [Opn->Sus]: Add str_slice() function, simple & logical string slicing function
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214646@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54387&edit=1 ID: 54387 Updated by: cmb@php.net Reported by: birken at gmail dot com Summary: Add str_slice() function, simple & logical string slicing function -Status: Open +Status: Suspended Type: Feature/Change Request Package: Strings related Operating System: agnostic PHP Version: 5.3SVN-2011-03-26 (SVN) Block user comment: N Private report: N New Comment: Thanks for the patch! Howver, adding yet more string functions to the standard library is controversial, especially when the respective functionality could easily be implemented in userland. Therefore, this feature requires the RFC process[1]. Please go ahead and start it! For the time being, I'm suspending this ticket. [1] <https://wiki.php.net/rfc/howto> Previous Comments: ------------------------------------------------------------------------ [2016-06-07 16:30:55] cmb@php.net Related to request #52129. ------------------------------------------------------------------------ [2011-03-26 10:19:18] birken at gmail dot com Description: ------------ The current state of string slicing in PHP is very confusing and non-obvious. The substr() function attempted some nice string slicing features, but it just isn't a good interface for string slicing and due to legacy concerns it cannot be changed (I assume). substr() problems: - The interface is inconsistent and confusing. When length is positive, then the function effectively returns from [start, start+length). When length is negative, the function returns [start, str_len+length). str_slice() has a much simpler interface: It returns from [start, end), and start and end may be defined as positive numbers from the start of the string or negative numbers from the end of the string. - Returning FALSE when start + length parameters are invalid. This is annoying because when using this function you always have to deal with this FALSE case if you need a string. Here is an example where str_slice() produces a more logical output than substr: print var_dump(substr('', -1) == '0'); // TRUE print var_dump(str_slice('', -1) == '0'); // FALSE This patch effectively solves the following bug as well: http://bugs.php.net/bug.php?id=38437 (substr should return '' instead of FALSE) http://bugs.php.net/bug.php?id=50434 (add str_startswith and str_endswith functions, which are trivial with str_slice, even in corner cases) Test script: --------------- Test is attached in the patch. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=54387&edit=1

« previous php.bugs (#214646) next »