rfc:add_str_begin_and_end_functions

This is an old revision of the document!


PHP RFC: Add str begin and end 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_begins() and str_ends() functions

boolean str_begins[(string $str, string $search_value [boolean $case_sensitive = true])
boolean str_ends[(string $str, string $search_value [boolean $case_sensitive = true])

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

By default, both str_begins() and str_ends() are case sensitive, however passing false as the third parameter to either function will cause PHP to perform a case insensitive comparison as opposed to a case sensitive one.

Examples below:

$str = "beginningMiddleEnd";
if (str_begins($str, "beg"))
    echo "This condition would be true";
 if (str_begins($str, "Beg"))
     echo "This condition would not be true";
 if (str_begins($str, "beg", false))
     echo "This condition would be true";
 if (str_begins($str, "Beg", false))
     echo "This condition would also be true";
 if (str_ends($str, "End"))
    echo "This condition would be true";
 if (str_ends($str, "end"))
     echo "This condition would not be true";
 if (str_ends($str, "End", false))
     echo "This condition would be true";
 if (str_ends($str, "end", false))
     echo "This condition would also be true";

Backward Incompatible Changes

None.

Proposed PHP Version(s)

Next PHP 7.x release.

RFC Impact

To SAPIs

Will add the aforementioned functions to all PHP environments.

To Existing Extensions

No.

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 only part of PHP that will be effected by this is the string library. This means that strings.c, basic_functions.c, and php_strings.h have all been modified to add the two new functions.

Future Scope

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

Proposed Voting Choices

This project requires a 2/3 majority to be approved.

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

Links to external references, discussions or RFCs

Rejected Features

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

rfc/add_str_begin_and_end_functions.1470144237.txt.gz · Last modified: 2017/09/22 13:28 (external edit)