|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-08-24 10:16 UTC] dagguh at gmail dot com
Description: ------------ --- From manual page: http://www.php.net/function.substr#refsect1-function.substr- description --- Truncating an entire string should result in a string. When $start is equal to strlen($string), an empty string should be returned instead of FALSE. Test script: --------------- var_dump(substr("", 0)); var_dump(substr("a", 1)); var_dump(substr("ab", 2)); Expected result: ---------------- string(0) "" string(0) "" string(0) "" Actual result: -------------- bool(false) bool(false) bool(false) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 00:00:01 2025 UTC |
Sheer logic. What remains from a 4-character string after cutting 4 characters? An empty string. In practice it would allow for a cleaner code, like: --------- public static function endsWith($string, $suffix) { $suffixLength = strlen($suffix); return $suffix === substr($string, -$suffixLength); } -------- Method endsWith returns true for: endsWith("kebab", "ebab"); endsWith("kebab", "bab"); endsWith("kebab", "ab"); endsWith("kebab", "b"); but it returns false for endsWith("kebab", "kebab");<? public static function endsWith($string, $suffix) { return preg_match("/{$suffix}$/", $string); } ?> No need to rewrite the language :)<? public static function endsWith($string, $suffix) { return 1 === preg_match("/{$suffix}$/", $string); } It should return a boolean :) Thanks :) ------ Still, it would be more logical if substr returned an empty string. I guess backward compatiblity is more important than consistency. From http://tr.php.net/manual/en/function.substr.php: If string is less than or equal to start characters long, FALSE will be returned. would become: If string is less than start characters long, FALSE will be returned.