Req #50434 [Com]: add string functions str_startswith and str_endswith for convenience
Edit report at https://bugs.php.net/bug.php?id=50434&edit=1 ID: 50434 Comment by: ajf at ajf dot me Reported by:mail at daniel-faber dot de Summary:add string functions str_startswith and str_endswith for convenience Status: Analyzed Type: Feature/Change Request Package:Strings related PHP Version:5.2.11 Block user comment: N Private report: N New Comment: olafvdspek: Oh, lots of us would like it, but it apparently adds unnecessary bloat so we obviously can't have it, even if it would help code readability and mean people would spend less time reinventing the wheel. Previous Comments: [2013-05-15 11:07:26] olafvdspek at gmail dot com Please? http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions Requests for these functions come up again and again. Case insensitive variants might be useful too. [2012-08-20 21:01:08] ajf at ajf dot me I would also like to see this. It's very handy for string checking. Both Python and C# have such a method. (accompanied by Python's in, it's very handy) For brevity I'd prefer startswith() and endswith(), however. I think I'll write a patch :) [2009-12-10 13:19:38] mail at daniel-faber dot de Possible problems with the above corner cases are not the main reasons why I requested these functions. Even if I know this corner cases will not occur in my application I'll prefer str_startswith($s, $p) to substr($s, 0, strlen($p)) == $p because it's shorter and easier to read. This functionality is so often needed and when you need it, you have do use the substr expression or define you own str_startswith function. So why not adding it to the PHP core since it's such a basic thing... [2009-12-10 00:58:48] mail at daniel-faber dot de rasmus, this is right in nearly all cases, but here are some corner cases where your code fails: startswith('', '0') should be false, but your code gives true reason: substr('', 0, 1) is false false == '0' is true if you try to fix this by replacing == with ===, another corner case will fail: startswith('', '') should be true, but will be false reason: substr('', 0, 0) is false false === '' is false this seems to work in all cases: function str_startswith($string, $prefix) { if (strlen($string) < strlen($prefix)) return false; return substr($string, 0, strlen($prefix)) == $prefix; } similar for endswith: endswith('', '0') should be false, but is true endswith('test', '') should be true, but is false this seems to work in all cases: function str_endswith($string, $suffix) { $suffixLength = strlen($suffix); if ($suffixLength == 0) return true; if (strlen($string) < $suffixLength) return false; return substr($string, -$suffixLength) == $suffix; } [2009-12-09 23:48:49] ras...@php.net Isn't it super trivial given the way substr works in PHP? startswith: substr($haystack,0,strlen($prefix)) == $prefix endswith: substr($haystack,-strlen($suffix)) == $suffix Usually we add things to core that can't be done with a single trivial call in PHP. The only thing you save is the strlen call, and you can hardcode that to avoid it. 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=50434 -- Edit this bug report at https://bugs.php.net/bug.php?id=50434&edit=1
Req #50434 [Com]: add string functions str_startswith and str_endswith for convenience
Edit report at https://bugs.php.net/bug.php?id=50434&edit=1 ID: 50434 Comment by: olafvdspek at gmail dot com Reported by:mail at daniel-faber dot de Summary:add string functions str_startswith and str_endswith for convenience Status: Analyzed Type: Feature/Change Request Package:Strings related PHP Version:5.2.11 Block user comment: N Private report: N New Comment: Please? http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions Requests for these functions come up again and again. Case insensitive variants might be useful too. Previous Comments: [2012-08-20 21:01:08] ajf at ajf dot me I would also like to see this. It's very handy for string checking. Both Python and C# have such a method. (accompanied by Python's in, it's very handy) For brevity I'd prefer startswith() and endswith(), however. I think I'll write a patch :) [2009-12-10 13:19:38] mail at daniel-faber dot de Possible problems with the above corner cases are not the main reasons why I requested these functions. Even if I know this corner cases will not occur in my application I'll prefer str_startswith($s, $p) to substr($s, 0, strlen($p)) == $p because it's shorter and easier to read. This functionality is so often needed and when you need it, you have do use the substr expression or define you own str_startswith function. So why not adding it to the PHP core since it's such a basic thing... [2009-12-10 00:58:48] mail at daniel-faber dot de rasmus, this is right in nearly all cases, but here are some corner cases where your code fails: startswith('', '0') should be false, but your code gives true reason: substr('', 0, 1) is false false == '0' is true if you try to fix this by replacing == with ===, another corner case will fail: startswith('', '') should be true, but will be false reason: substr('', 0, 0) is false false === '' is false this seems to work in all cases: function str_startswith($string, $prefix) { if (strlen($string) < strlen($prefix)) return false; return substr($string, 0, strlen($prefix)) == $prefix; } similar for endswith: endswith('', '0') should be false, but is true endswith('test', '') should be true, but is false this seems to work in all cases: function str_endswith($string, $suffix) { $suffixLength = strlen($suffix); if ($suffixLength == 0) return true; if (strlen($string) < $suffixLength) return false; return substr($string, -$suffixLength) == $suffix; } [2009-12-09 23:48:49] ras...@php.net Isn't it super trivial given the way substr works in PHP? startswith: substr($haystack,0,strlen($prefix)) == $prefix endswith: substr($haystack,-strlen($suffix)) == $suffix Usually we add things to core that can't be done with a single trivial call in PHP. The only thing you save is the strlen call, and you can hardcode that to avoid it. [2009-12-09 23:39:58] mail at daniel-faber dot de Description: Please add these two string functions for convenience: boolean str_startswith($string, $prefix) // true if $string starts with $prefix, false otherwise boolean str_endswith($string, $suffix) // true if $string ends with $suffix, false otherwise Of course one can easily write these functions in PHP, but having them in the PHP core would be nice. You find them in other languages too: Java: string.startsWith(prefix) and string.endsWith(suffix) Python: string.startswith(prefix) and string.endswith(suffix) -- Edit this bug report at https://bugs.php.net/bug.php?id=50434&edit=1
Req #50434 [Com]: add string functions str_startswith and str_endswith for convenience
Edit report at https://bugs.php.net/bug.php?id=50434&edit=1 ID: 50434 Comment by: ajf at ajf dot me Reported by:mail at daniel-faber dot de Summary:add string functions str_startswith and str_endswith for convenience Status: Analyzed Type: Feature/Change Request Package:Strings related PHP Version:5.2.11 Block user comment: N Private report: N New Comment: I would also like to see this. It's very handy for string checking. Both Python and C# have such a method. (accompanied by Python's in, it's very handy) For brevity I'd prefer startswith() and endswith(), however. I think I'll write a patch :) Previous Comments: [2009-12-10 13:19:38] mail at daniel-faber dot de Possible problems with the above corner cases are not the main reasons why I requested these functions. Even if I know this corner cases will not occur in my application I'll prefer str_startswith($s, $p) to substr($s, 0, strlen($p)) == $p because it's shorter and easier to read. This functionality is so often needed and when you need it, you have do use the substr expression or define you own str_startswith function. So why not adding it to the PHP core since it's such a basic thing... [2009-12-10 00:58:48] mail at daniel-faber dot de rasmus, this is right in nearly all cases, but here are some corner cases where your code fails: startswith('', '0') should be false, but your code gives true reason: substr('', 0, 1) is false false == '0' is true if you try to fix this by replacing == with ===, another corner case will fail: startswith('', '') should be true, but will be false reason: substr('', 0, 0) is false false === '' is false this seems to work in all cases: function str_startswith($string, $prefix) { if (strlen($string) < strlen($prefix)) return false; return substr($string, 0, strlen($prefix)) == $prefix; } similar for endswith: endswith('', '0') should be false, but is true endswith('test', '') should be true, but is false this seems to work in all cases: function str_endswith($string, $suffix) { $suffixLength = strlen($suffix); if ($suffixLength == 0) return true; if (strlen($string) < $suffixLength) return false; return substr($string, -$suffixLength) == $suffix; } [2009-12-09 23:48:49] ras...@php.net Isn't it super trivial given the way substr works in PHP? startswith: substr($haystack,0,strlen($prefix)) == $prefix endswith: substr($haystack,-strlen($suffix)) == $suffix Usually we add things to core that can't be done with a single trivial call in PHP. The only thing you save is the strlen call, and you can hardcode that to avoid it. [2009-12-09 23:39:58] mail at daniel-faber dot de Description: Please add these two string functions for convenience: boolean str_startswith($string, $prefix) // true if $string starts with $prefix, false otherwise boolean str_endswith($string, $suffix) // true if $string ends with $suffix, false otherwise Of course one can easily write these functions in PHP, but having them in the PHP core would be nice. You find them in other languages too: Java: string.startsWith(prefix) and string.endsWith(suffix) Python: string.startswith(prefix) and string.endswith(suffix) -- Edit this bug report at https://bugs.php.net/bug.php?id=50434&edit=1