Req #62922 [Opn->Csd]: Truncating entire string should result in string
| From: | nikic@php.net | Date: | Sat, 20 Jun 2015 12:41:44 +0000 |
| Subject: | Req #62922 [Opn->Csd]: Truncating entire string should result in string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-193715@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62922&edit=1
ID: 62922
Updated by: nikic@php.net
Reported by: dagguh at gmail dot com
Summary: Truncating entire string should result in string
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Strings related
PHP Version: 5.3.16
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=257054e81d4dad73bf9d09cd206d3a6727ad1777
Log: Fix bug #62922
Previous Comments:
------------------------------------------------------------------------
[2012-08-24 14:43:26] riptide dot tempora at opinehub dot com
Unless there is some specific compatibility issue that would be raised if your change were
implemented, I agree.
------------------------------------------------------------------------
[2012-08-24 14:31:06] dagguh at gmail dot com
<?
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.
------------------------------------------------------------------------
[2012-08-24 14:24:31] riptide dot tempora at opinehub dot com
<?
public static function endsWith($string, $suffix) {
return preg_match("/{$suffix}$/", $string);
}
?>
No need to rewrite the language :)
------------------------------------------------------------------------
[2012-08-24 14:21:21] dagguh at gmail dot com
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");
------------------------------------------------------------------------
[2012-08-24 12:28:43] laruence@php.net
what can we gain from changing this? except the bc break?
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=62922
--
Edit this bug report at https://bugs.php.net/bug.php?id=62922&edit=1