ID: 41375 Updated by: [EMAIL PROTECTED] Reported By: bugs dot php dot net at kreidenweis dot com Status: Open -Bug Type: Strings related +Bug Type: Documentation problem Operating System: Any PHP Version: 5.2.2 New Comment:
Reclassified. Previous Comments: ------------------------------------------------------------------------ [2007-09-07 06:48:16] [EMAIL PROTECTED] "If start is negative, the returned string will start at the start'th character from the end of string." The function still accepts a negative start. What changed is if the start is longer than the string length, the test (user friendly version): if (from > strlen(str) || (from < 0 && -from > strlen(str))) As I agree it may break some scripts, the new behavior should be documented, as a bug fix. Can we move this bug to the doc category? ------------------------------------------------------------------------ [2007-09-07 02:22:57] [EMAIL PROTECTED] Seems to me as if this was introduced with 5.2.3, not 5.2.2 per the original submitter. Also see http://us.php.net/manual/en/function.substr.php#75999 - this isn't something that only affects me. ------------------------------------------------------------------------ [2007-09-07 02:20:36] [EMAIL PROTECTED] I'm sorry, but this is a definite BC break and has bitten me both in Horde code and in my company's codebase. I've re-read the substr docs several times and I don't see any indication that a negative $start can cause substr to return false. Here's the description of the start parameter in its entirety: If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth. If start is negative, the returned string will start at the start'th character from the end of string. Example 2454. Using a negative start <?php $rest = substr("abcdef", -1); // returns "f" $rest = substr("abcdef", -2); // returns "ef" $rest = substr("abcdef", -3, 1); // returns "d" ... from http://us3.php.net/substr. ------------------------------------------------------------------------ [2007-05-12 15:47:23] [EMAIL PROTECTED] Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php . ------------------------------------------------------------------------ [2007-05-12 12:52:22] bugs dot php dot net at kreidenweis dot com Description: ------------ substr() handled a negative $start parameter with an absolute value larger than the $string's length gracefully and sensibly up to PHP 5.2.1 In 5.2.2 this behavior changed without that being mentioned in the documentation. IMHO it probably has something to do with the fix for http://bugs.php.net/bug.php?id=40754 I guess (and hope ;) the behavior change wasn't intended. But if indeed so, please put a warning in the documentation. Reproduce code: --------------- var_dump(substr('abc', -4)); Expected result: ---------------- string(3) "abc" (up to PHP 5.2.1 this is what happens) Actual result: -------------- bool(false) (output by PHP 5.2.2) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41375&edit=1