rfc:add_str_starts_with_and_ends_with_functions

This is an old revision of the document!


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

Introduction

str_starts_with checks if a string, s, begins with another string, s′ and returns a boolean value (true/false) if s starts with s′. str_ends_with checks if a a string, s, ends with another string, s′ and returns a boolean value (true/false) if s ends with s′.

Typically this functionality is accomplished by repurposing existing string functions such as 'substr', 'strncmp', 'strpos', or 'substr_compare'. These bespoke userland implementations have various downsides, discussed later in this RFC.

The str_starts_with and str_ends_with functionality is so important, and so problematic to leave to userland, that many major PHP frameworks support it including Symfony, Laravel,Yii, FuelPHP, and Phalcon. Please note, the links are for str_starts_with functionality, but they all also contain str_ends_with functionality.

Checking the start and end of strings is a very common task which should be easy. Accomplishing this task is not easy now and that is why many frameworks have chosen to include it. This is also why other programming languages, as diverse as JavaScript, Java, Haskell, and Matlab, have also implemented this functionality. Checking the start and end of a string should not be a task which requires pulling in a PHP framework or developing a potentially suboptimal function in userland.

Proposal

Add two new basic functions: str_starts_with and str_ends_with:

str_starts_with ( string $haystack , string $needle ) : bool
str_ends_with ( string $haystack , string $needle ) : bool

str_starts_with() checks if $haystack begins with $needle. If $needle is longer than $haystack, it returns false; else, it compares each character in $needle with the corresponding character in $haystack (aligning both strings at their start), returning false if it encounters a mismatch, and true otherwise.
str_ends_with() does the same thing but aligning both strings at their end.

Examples below:

$str = "beginningMiddleEnd";
if (str_starts_with($str, "beg")) echo "printed\n";
if (str_starts_with($str, "Beg")) echo "not printed\n";
if (str_ends_with($str, "End")) echo "printed\n";
if (str_ends_with($str, "end")) echo "not printed\n";
 
// empty strings:
if (str_starts_with("a", "")) echo "printed\n";
if (str_starts_with("", "")) echo "printed\n";
if (str_starts_with("", "a")) echo "not printed\n";
if (str_ends_with("a", "")) echo "printed\n";
if (str_ends_with("", "")) echo "printed\n";
if (str_ends_with("", "a")) echo "not printed\n";
 

Please note, the behavior concerning empty strings is in accordance with the behavior of the accepted str_contains RFC. This behavior is also the same as is common with other languages, including Java and Python.

Case-insensitivity and multibyte strings

Previously I raised an RFC, here, proposing not just the addition of str_starts_with and str_ends_with, but also case-insensitive and multi-byte versions. On the vote for whether to add str_starts_with, str_starts_with_ci, str_ends_with, and str_ends_with_ci to PHP 7.4“, 27 people voted “Yes” and 20 voted “No.” Several “no” voters indicated that the reason they voted against the RFC was the inclusion of the case-insensitive versions. There were concerns over the need for the case-sensitive versions as well as their naming.

Further, a separate vote on the inclusion of multibyte versions of the functions carried 4 “Yes” votes compared to 37 “No” votes.

In keeping with the precedent of the str_contains RFC on the matter, here, it is proposed that only the simple functions of str_starts_with and str_ends_with be included at this time. Further case-insensitive or multibyte versions would be a future consideration.

Backward Incompatible Changes

This could break functions existing in userland with the same names. But see the corresponding section in the str_contains RFC for a discussion illustrating how this concern may be mitigated and why this concern does not justify the rejection of this RFC.

Proposed PHP Version(s)

PHP 8

RFC Impact

  • To SAPIs: Will add the aforementioned functions to all PHP environments.
  • To Existing Extensions: None.
  • To Opcache: No effect.
  • New Constants: No new constants.
  • php.ini Defaults: No changed php.ini settings.

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

Rejected Features

rfc/add_str_starts_with_and_ends_with_functions.1586018179.txt.gz · Last modified: 2020/04/04 16:36 by wkhudgins92