|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67035 wish: str_ends() and str_begins()
Submitted: 2014-04-06 15:32 UTC Modified: 2018-03-10 13:51 UTC
Avg. Score:1.7 ± 0.9
Reproduced:1 of 3 (33.3%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: php at richardneill dot org Assigned: cmb (profile)
Status: Duplicate Package: Strings related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2014-04-06 15:32 UTC] php at richardneill dot org
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);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-14 12:59 UTC] malalaimo923 at gmail dot com
Please see this document:

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-14 13:26 UTC] 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")){ ...
    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.
 [2018-03-10 13:51 UTC]
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2018-03-10 13:51 UTC]
Duplicate of feature request #50434.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 23 05:01:29 2024 UTC