Table of Contents

PHP RFC: Add str_starts_with(), str_ends_with() and related functions

Introduction

PHP does not contain functions to test if a string begins or ends with a certain substring. This is currently possible using several other functions, but adding pre-built functions to do this will improve the readability and clarity of PHP code using the function. This feature has been requested multiple times and would be of use to many PHP developers with varying levels of experience.

Proposal

Add str_starts_with(), str_starts_with_ci(), str_ends_with(), str_ends_with_ci(), mb_str_starts_with(), mb_str_starts_with_ci(), mb_str_ends_with(), and mb_str_ends_with_ci() functions:

function str_starts_with(string $haystack, string $needle): bool
function str_starts_with_ci(string $haystack, string $needle): bool
function str_ends_with(string $haystack, string $needle): bool
function str_ends_with_ci(string $haystack, string $needle): bool
function mb_str_starts_with(string $haystack, string $needle [, string $encoding]): bool
function mb_str_starts_with_ci(string $haystack, string $needle [, string $encoding]): bool
function mb_str_ends_with(string $haystack, string $needle [, string $encoding]): bool
function mb_str_ends_with_ci(string $haystack, string $needle [, string $encoding]): bool

str_starts_with() checks if $haystack begins with $needle. It accomplishes this by comparing each character in $haystack with the corresponding character in $needle. If any of the characters do not match, it will return false. str_ends_with() does the same thing except in reverse: it starts at the end of both $haystack and $needle and compares each character in $haystack to the corresponding character in $needle.

str_starts_with_ci() and str_ends_with_ci() do the same thing, except they are case insensitive.

The mb_* versions of these method function very similar except they make use of the mbfl_strpos() function or the php_mb_stripos() helper function.

Examples below:

$str = "beginningMiddleEnd";
if (str_starts_with($str, "beg"))
    echo "This condition would be true";
if (str_starts_with($str, "Beg"))
    echo "This condition would not be true";
if (str_starts_with_ci($str, "beg"))
    echo "This condition would be true";
if (str_starts_with_ci($str, "Beg"))
    echo "This condition would also be true";
if (str_ends_with($str, "End"))
    echo "This condition would be true";
if (str_ends_with($str, "end"))
    echo "This condition would not be true";
if (str_ends_with_ci($str, "End"))
    echo "This condition would be true";
if (str_ends_with_ci($str, "end"))
    echo "This condition would also be true";

Backward Incompatible Changes

This could break functions existing in userland with the same names.

Proposed PHP Version(s)

Next eligible PHP 7.x release.

RFC Impact

To SAPIs

Will add the aforementioned functions to all PHP environments.

To Existing Extensions

Adds mb_str_starts_with(), mb_str_starts_with_ci(), mb_str_ends_with(), and mb_str_ends_with_ci() to the mbstring extension.

To Opcache

No effect.

New Constants

No new constants.

php.ini Defaults

No changed php.ini settings.

Open Issues

This functionality was requested in both of these bug reports: https://bugs.php.net/bug.php?id=67035 and https://bugs.php.net/bug.php?id=50434.

Unaffected PHP Functionality

The PHP string library has been modified. This means that strings.c, basic_functions.c, and php_strings.h have all been modified. Additionally the mbstring library has been modified.

Future Scope

Once this feature is approved and added, it will not need any future improvements.

Vote

Voting closes 2019-07-20.

Do you want str_starts_with, str_starts_with_ci, str_ends_with, and str_ends_with_ci functions in PHP 7.4?
Real name yes no
aeoris (aeoris)  
andrey (andrey)  
ashnazg (ashnazg)  
beberlei (beberlei)  
bishop (bishop)  
brzuchal (brzuchal)  
carusogabriel (carusogabriel)  
cpriest (cpriest)  
daverandom (daverandom)  
derick (derick)  
dmitry (dmitry)  
duncan3dc (duncan3dc)  
galvao (galvao)  
gasolwu (gasolwu)  
girgias (girgias)  
jasny (jasny)  
jhdxr (jhdxr)  
kalle (kalle)  
kelunik (kelunik)  
kguest (kguest)  
kinncj (kinncj)  
krakjoe (krakjoe)  
kriscraig (kriscraig)  
mbeccati (mbeccati)  
mfonda (mfonda)  
mike (mike)  
narf (narf)  
nikic (nikic)  
ocramius (ocramius)  
peehaa (peehaa)  
petk (petk)  
pmjones (pmjones)  
ramsey (ramsey)  
reywob (reywob)  
rjhdby (rjhdby)  
rtheunissen (rtheunissen)  
salathe (salathe)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
stas (stas)  
subjective (subjective)  
svpernova09 (svpernova09)  
thorstenr (thorstenr)  
trowski (trowski)  
wjx (wjx)  
yunosh (yunosh)  
zeev (zeev)  
Count: 27 20

Do you want mb_str_starts_with, mb_str_starts_with_ci, mb_str_ends_with, and mb_str_ends_with_ci functions in PHP 7.4?
Real name yes no
aeoris (aeoris)  
andrey (andrey)  
ashnazg (ashnazg)  
beberlei (beberlei)  
bishop (bishop)  
brzuchal (brzuchal)  
carusogabriel (carusogabriel)  
cmb (cmb)  
daverandom (daverandom)  
derick (derick)  
dmitry (dmitry)  
duncan3dc (duncan3dc)  
galvao (galvao)  
gasolwu (gasolwu)  
girgias (girgias)  
jasny (jasny)  
jhdxr (jhdxr)  
kalle (kalle)  
kguest (kguest)  
kinncj (kinncj)  
krakjoe (krakjoe)  
kriscraig (kriscraig)  
narf (narf)  
nikic (nikic)  
ocramius (ocramius)  
petk (petk)  
pollita (pollita)  
ramsey (ramsey)  
reywob (reywob)  
rjhdby (rjhdby)  
rtheunissen (rtheunissen)  
salathe (salathe)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
stas (stas)  
subjective (subjective)  
svpernova09 (svpernova09)  
thorstenr (thorstenr)  
wjx (wjx)  
yunosh (yunosh)  
zeev (zeev)  
Count: 4 37

Patches and Tests

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

Implementation of similar methods/functions in other languages:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

https://docs.python.org/3/library/stdtypes.html?highlight=startswith#str.startswith

https://docs.python.org/3/library/stdtypes.html?highlight=startswith#str.endswith

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith(java.lang.String)

http://ruby-doc.org/core-2.1.1/String.html#method-i-start_with-3F

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#startsWithIgnoreCase-java.lang.CharSequence-java.lang.CharSequence-

https://golang.org/pkg/strings/#HasPrefix

https://hackage.haskell.org/package/MissingH-1.4.0.1/docs/Data-String-Utils.html#v:startswith

Related feature request: bug #50434.

Rejected Features

Keep this updated with features that were discussed on the mail lists.