Req #54387 [NEW]: Add str_slice() function, simple & logical string slicing function
| From: | birken at gmail dot com | Date: | Sat, 26 Mar 2011 09:19:19 +0000 |
| Subject: | Req #54387 [NEW]: Add str_slice() function, simple & logical string slicing function | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-159790@lists.php.net to get a copy of this message | ||
From:
Operating system: agnostic
PHP version: 5.3SVN-2011-03-26 (SVN)
Package: Strings related
Bug Type: Feature/Change Request
Bug description:Add str_slice() function, simple & logical string slicing function
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 bug report at http://bugs.php.net/bug.php?id=54387&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=54387&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=54387&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=54387&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=54387&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=54387&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=54387&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=54387&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=54387&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=54387&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=54387&r=support
Expected behavior: http://bugs.php.net/fix.php?id=54387&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=54387&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=54387&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=54387&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54387&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=54387&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=54387&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=54387&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=54387&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=54387&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=54387&r=mysqlcfg