|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-04-06 15:32 UTC] 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);
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 07:00:02 2025 UTC |
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.