Take the State of PHP 2026 Survey →

Add str_starts_with(), str_ends_with() and related functions View as Markdown

Declined
Authors
Will Hudgins, will@wkhudgins.info
Date
2016-08-01 (use today's date here) Updated: 2019-06-18
Version
0.4
Source
wiki.php.net/rfc/add_str_begin_and_end_functions

Votes

Do you want str_starts_with, str_starts_with_ci, str_ends_with, and str_ends_with_ci functions in PHP 7.4? closed
yes
27
no
20
47 voters
aeoris yes 2019-07-17 07:48
andrey yes 2019-07-22 15:28
ashnazg yes 2019-07-10 15:22
beberlei yes 2019-07-05 12:28
bishop yes 2019-07-09 18:26
brzuchal no 2019-07-15 11:18
carusogabriel yes 2019-07-07 21:03
cpriest no 2019-07-09 21:34
daverandom no 2019-07-08 11:17
derick yes 2019-07-09 14:53
dmitry no 2019-07-11 12:27
duncan3dc yes 2019-07-05 15:05
galvao no 2019-07-05 16:20
gasolwu yes 2019-07-08 04:23
girgias no 2019-07-10 20:27
jasny no 2019-07-07 19:48
jhdxr yes 2019-07-09 07:13
kalle no 2019-07-05 12:01
kelunik yes 2019-07-05 20:54
kguest yes 2019-07-15 13:34
kinncj no 2019-07-08 02:33
krakjoe no 2019-07-09 07:49
kriscraig yes 2019-07-08 13:42
mbeccati yes 2019-07-08 09:39
mfonda no 2019-07-08 22:03
mike yes 2019-07-05 09:43
narf no 2019-07-05 09:55
nikic yes 2019-07-05 08:32
ocramius yes 2019-07-05 08:46
peehaa no 2019-07-09 00:17
petk yes 2019-07-05 13:22
pmjones yes 2019-07-08 14:41
ramsey yes 2019-07-07 21:19
reywob no 2019-07-05 09:01
rjhdby no 2019-07-05 08:41
rtheunissen no 2019-07-08 23:30
salathe no 2019-07-05 11:17
santiagolizardo no 2019-07-09 22:21
sergey no 2019-07-05 19:05
stas yes 2019-07-07 21:42
subjective yes 2019-07-08 10:36
svpernova09 yes 2019-07-09 13:38
thorstenr yes 2019-07-11 08:23
trowski yes 2019-07-08 05:13
wjx yes 2019-07-08 05:37
yunosh yes 2019-07-05 07:23
zeev no 2019-07-08 14:21
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? closed
yes
4
no
37
41 voters
aeoris yes 2019-07-17 07:56
andrey no 2019-07-22 15:26
ashnazg yes 2019-07-10 15:22
beberlei no 2019-07-05 12:28
bishop no 2019-07-09 17:54
brzuchal no 2019-07-15 11:18
carusogabriel no 2019-07-07 21:03
cmb no 2019-07-05 12:38
daverandom no 2019-07-08 11:17
derick no 2019-07-09 14:53
dmitry no 2019-07-11 12:28
duncan3dc no 2019-07-05 15:05
galvao no 2019-07-05 16:20
gasolwu no 2019-07-08 04:28
girgias no 2019-07-10 20:27
jasny no 2019-07-07 19:48
jhdxr no 2019-07-09 07:13
kalle no 2019-07-05 12:01
kguest no 2019-07-15 13:34
kinncj no 2019-07-08 02:34
krakjoe no 2019-07-09 07:49
kriscraig no 2019-07-08 13:42
narf no 2019-07-05 09:55
nikic no 2019-07-05 08:30
ocramius no 2019-07-08 14:13
petk yes 2019-07-05 13:22
pollita no 2019-07-08 01:18
ramsey no 2019-07-07 21:20
reywob no 2019-07-05 09:01
rjhdby no 2019-07-05 08:42
rtheunissen no 2019-07-08 23:31
salathe no 2019-07-05 11:17
santiagolizardo no 2019-07-09 22:21
sergey no 2019-07-05 19:04
stas no 2019-07-10 21:47
subjective no 2019-07-08 10:36
svpernova09 no 2019-07-09 13:38
thorstenr no 2019-07-11 08:23
wjx no 2019-07-08 05:37
yunosh yes 2019-07-05 07:23
zeev no 2019-07-08 14:17

Discussions on the mailing list

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

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.

Vote: Do you want str_starts_with, str_starts_with_ci, str_ends_with, and str_ends_with_ci functions in PHP 7.4? — yes: 27 · no: 20 (closed)


Vote: 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? — yes: 4 · no: 37 (closed)

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.