rfc:add_str_starts_with_and_ends_with_functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:add_str_starts_with_and_ends_with_functions [2020/04/05 15:56] – typo guilliamxavierrfc:add_str_starts_with_and_ends_with_functions [2020/05/05 14:12] (current) nikic
Line 1: Line 1:
 ====== PHP RFC: Add str_starts_with() and str_ends_with() functions ====== ====== PHP RFC: Add str_starts_with() and str_ends_with() functions ======
   * Version: 0.5   * Version: 0.5
-  * Date: 2020-03-25 (**Updated**: 2020-04-05)+  * Date: 2020-03-25 (**Updated**: 2020-05-05)
   * Author: Will Hudgins, will@wkhudgins.info   * Author: Will Hudgins, will@wkhudgins.info
-  * Status: Under Discussion+  * Status: Implemented
   * First Published at: https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions   * First Published at: https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions
  
Line 14: Line 14:
 The ''str_starts_with'' and ''str_ends_with'' functionality is so commonly needed that many major PHP frameworks support it, including [[https://symfony.com/doc/5.0/components/string.html#methods-to-search-and-replace|Symfony]], [[https://laravel.com/docs/7.x/helpers#method-starts-with|Laravel]], [[https://www.yiiframework.com/doc/api/2.0/yii-helpers-basestringhelper#startsWith()-detail|Yii]], [[https://fuelphp.com/docs/classes/str.html#/method_starts_with|FuelPHP]], and [[https://docs.phalcon.io/3.4/en/api/phalcon_text|Phalcon]] ((some of those links are for ''str_starts_with'' functionality, but the mentioned frameworks also contain ''str_ends_with'' functionality, often visible on the same web page)). The ''str_starts_with'' and ''str_ends_with'' functionality is so commonly needed that many major PHP frameworks support it, including [[https://symfony.com/doc/5.0/components/string.html#methods-to-search-and-replace|Symfony]], [[https://laravel.com/docs/7.x/helpers#method-starts-with|Laravel]], [[https://www.yiiframework.com/doc/api/2.0/yii-helpers-basestringhelper#startsWith()-detail|Yii]], [[https://fuelphp.com/docs/classes/str.html#/method_starts_with|FuelPHP]], and [[https://docs.phalcon.io/3.4/en/api/phalcon_text|Phalcon]] ((some of those links are for ''str_starts_with'' functionality, but the mentioned frameworks also contain ''str_ends_with'' functionality, often visible on the same web page)).
  
-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 high-level programming languages---diverse as JavaScript, Java, Haskell, and Matlab---have 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 (or worse, buggy) function in userland.+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 high-level programming languages---as diverse as JavaScript, Java, Haskell, and Matlab---have 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 (or worse, buggy) function in userland.
  
 ==== Downsides of Common Userland Approaches ===== ==== Downsides of Common Userland Approaches =====
-Ad hoc userland implementations of this functionality are //less intuitive// than dedicated functions (this is especially true for new PHP developers and developers who frequently switch between PHP and other languages---many of which include this functionality natively).\\ +Ad hoc userland implementations of this functionality are __less intuitive__ than dedicated functions (this is especially true for new PHP developers and developers who frequently switch between PHP and other languages---many of which include this functionality natively).\\ 
-The implementation is also //easy to get wrong// (especially with the ''==='' comparison).\\ +The implementation is also __easy to get wrong__ (especially with the ''==='' comparison).\\ 
-Additionally, there are //performance issues// with many userland implementations.+Additionally, there are __performance issues__ with many userland implementations.
  
-Note: the examples below are sometimes additionally guarded by "<php>$needle === "" || </php>" and/or "<php>strlen($needle) <= strlen($haystack) && </php>" to handle empty string and/or avoid warnings.+//Note: some implementations add "//<php>$needle === "" || </php>//" and/or "//<php>strlen($needle) <= strlen($haystack) && </php>//guards to handle empty needle values and/or avoid warnings.//
  
 === str_starts_with === === str_starts_with ===
Line 49: Line 49:
 strpos(strrev($haystack), strrev($needle)) === 0 strpos(strrev($haystack), strrev($needle)) === 0
 </PHP> </PHP>
-This is CPU inefficient because it requires reversing both the haystack and the needle as well as applying ''strpos'' (see above).+This is doubly inefficient because it requires reversing both the haystack and the needle as well as applying ''strpos'' (see above).
  
 <PHP> <PHP>
Line 105: Line 105:
   * **New Constants:** No new constants.   * **New Constants:** No new constants.
   * **php.ini Defaults:** No changed php.ini settings.   * **php.ini Defaults:** No changed php.ini settings.
 +
 +===== Votes =====
 +Voting closes 2020-05-04
 +
 +<doodle 
 +title="Add str_starts_with and str_ends_with as described" auth="wkhudgins92" voteType="single" closed="true">
 +   * yes
 +   * no
 +</doodle>
 +
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 132: Line 142:
  
 ===== Rejected Features ===== ===== Rejected Features =====
-  * Case-insensitive and multibyte variants were included in the previous version of this RFC, which was declined. See also [[rfc:str_contains#case-insensitivity_and_multibyte_strings|the related section in the str_contains RFC]].+  * **Case-insensitive** and **multibyte** variants were included in the previous version of this RFC, which was declined. See also [[rfc:str_contains#case-insensitivity_and_multibyte_strings|the related section in the str_contains RFC]].
rfc/add_str_starts_with_and_ends_with_functions.1586102203.txt.gz · Last modified: 2020/04/05 15:56 by guilliamxavier