php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62922 Truncating entire string should result in string
Submitted: 2012-08-24 10:16 UTC Modified: 2012-08-24 12:28 UTC
From: dagguh at gmail dot com Assigned:
Status: Closed Package: Strings related
PHP Version: 5.3.16 OS:
Private report: No CVE-ID: None
 [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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-24 12:28 UTC] laruence@php.net
what can we gain from changing this? except the bc break?
 [2012-08-24 14:21 UTC] 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 14:24 UTC] 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:31 UTC] 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:43 UTC] 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.
 [2015-06-20 12:41 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=257054e81d4dad73bf9d09cd206d3a6727ad1777
Log: Fix bug #62922
 [2015-06-20 12:41 UTC] nikic@php.net
-Status: Open +Status: Closed
 [2015-06-23 18:04 UTC] ab@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=257054e81d4dad73bf9d09cd206d3a6727ad1777
Log: Fix bug #62922
 [2016-07-20 11:38 UTC] davey@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=257054e81d4dad73bf9d09cd206d3a6727ad1777
Log: Fix bug #62922
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC