Req #67035 [Opn->Dup]: wish: str_ends() and str_begins()
| From: | cmb@php.net | Date: | Sat, 10 Mar 2018 13:51:41 +0000 |
| Subject: | Req #67035 [Opn->Dup]: wish: str_ends() and str_begins() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214263@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67035&edit=1
ID: 67035
Updated by: cmb@php.net
Reported by: php at richardneill dot org
Summary: wish: str_ends() and str_begins()
-Status: Open
+Status: Duplicate
Type: Feature/Change Request
Package: Strings related
PHP Version: Irrelevant
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Duplicate of feature request #50434.
Previous Comments:
------------------------------------------------------------------------
[2014-04-14 13:26:40] php at richardneill dot org
Hello, and thanks for your comment. That wasn't quite what I meant. I intended the functions to
return a boolean, (as per my examples in the original request). Not "str_end()" which
would return the final character, but "str_ends()", i.e. "does this string end with
that string".
For example:
if (str_ends($filename, ".jpg")){ ...
or
if (str_begins($url, "https://")){ ...
I do realise these are relatively trivial, but given how common this kind of construction is, I
think they'd be useful to lots of people as a convenience.
------------------------------------------------------------------------
[2014-04-14 12:59:16] malalaimo923 at gmail dot com
Please see this document: http://www.php.net/manual/en/language.types.string.php
It is possible to access and modify a character of a string using an index.
The String End is: $string[strlen($string)-1];
The String Start is: $string[0];
------------------------------------------------------------------------
[2014-04-06 15:32:04] php at richardneill dot org
Description:
------------
It would be really useful to have standard functions:
str_ends() and str_begins()
These are trivial to implement, but the use for them is so extremely common that they'd be a
helpful addition to the standard library. It's also not quite obvious to beginners how to do it
- especially for str_ends().
For comparison, Javascript has string.startsWith() and string.endsWith()
Examples below. Thank you for your consideration and your time.
Test script:
---------------
function str_ends($string,$end){
return (substr($string,-strlen($end),strlen($end)) === $end);
}
function str_begins($string,$start){
return (substr($string,0,strlen($start)) === $start);
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67035&edit=1