rfc:add_str_begin_and_end_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_begin_and_end_functions [2016/08/13 00:27] – Split str_begins() and str_ends() into str_begin(), str_ibegin(), str_end(), and str_iend() and changed parameter order for consistency wkhudgins92rfc:add_str_begin_and_end_functions [2019/07/22 08:47] (current) – Close voting nikic
Line 1: Line 1:
-====== PHP RFC: Add str begin and end functions ====== +====== PHP RFC: Add str_starts_with(), str_ends_with() and related functions ====== 
-  * Version: 0.1 +  * Version: 0.4 
-  * Date: 2016-08-01 (use today's date here)+  * Date: 2016-08-01 (use today's date here) **Updated: 2019-06-18**
   * Author: Will Hudgins, will@wkhudgins.info   * Author: Will Hudgins, will@wkhudgins.info
-  * Status: Under Discussion+  * Status: Declined
   * First Published at: https://wiki.php.net/rfc/add_str_begin_and_end_functions   * First Published at: https://wiki.php.net/rfc/add_str_begin_and_end_functions
  
Line 10: Line 10:
  
 ===== Proposal ===== ===== Proposal =====
-Add str_begin(), str_ibegin(), str_end(), and str_iend() functions+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:
  
-  boolean str_begin[(string $search_value, string $str)] +<PHP> 
-  boolean str_ibegin[(string $search_value, string $str)] +function str_starts_with(string $haystack, string $needle): bool 
-  boolean str_end[(string $search_value, string $str)] +function str_starts_with_ci(string $haystack, string $needle): bool 
-  boolean str_iend[(string $search_value, string $str)]+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 
 +</PHP> 
 +   
 +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_begin() 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_end() 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.+str_starts_with_ci() and str_ends_with_ci() do the same thingexcept they are case insensitive.
  
-str_ibegin() and str_iend() 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: Examples below:
-  $str = "beginningMiddleEnd"; + 
-  if (str_begin("beg", $str)) +<PHP> 
-      echo "This condition would be true"; +$str = "beginningMiddleEnd"; 
-   if (str_begins("Beg", $str)) +if (str_starts_with($str, "beg")) 
-       echo "This condition would not be true"; +    echo "This condition would be true"; 
-   if (str_ibegin("beg", $str)) +if (str_starts_with($str, "Beg")) 
-       echo "This condition would be true"; +    echo "This condition would not be true"; 
-   if (str_ibegin("Beg", $str)) +if (str_starts_with_ci($str, "beg")) 
-       echo "This condition would also be true"; +    echo "This condition would be true"; 
-   if (str_end("End", $str)) +if (str_starts_with_ci($str, "Beg")) 
-      echo "This condition would be true"; +    echo "This condition would also be true"; 
-   if (str_end("end", $str)) +if (str_ends_with($str, "End")) 
-       echo "This condition would not be true"; +    echo "This condition would be true"; 
-   if (str_iend("End", $str)) +if (str_ends_with($str, "end")) 
-       echo "This condition would be true"; +    echo "This condition would not be true"; 
-   if (str_iend("end", $str)) +if (str_ends_with_ci($str, "End")) 
-       echo "This condition would also be true";+    echo "This condition would be true"; 
 +if (str_ends_with_ci($str, "end")) 
 +    echo "This condition would also be true"; 
 +</PHP>
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-None.+This could break functions existing in userland with the same names.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-Next PHP 7.x release.+Next eligible PHP 7.x release.
  
 ===== RFC Impact ===== ===== RFC Impact =====
Line 51: Line 62:
  
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
-No.+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 ==== ==== To Opcache ====
Line 67: Line 78:
  
 ===== Unaffected PHP Functionality ===== ===== 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.+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 ===== ===== Future Scope =====
 +
 Once this feature is approved and added, it will not need any future improvements. Once this feature is approved and added, it will not need any future improvements.
  
-===== Proposed Voting Choices ===== +===== Vote ===== 
-This project requires a 2/3 majority to be approved.+ 
 +Voting closes 2019-07-20. 
 + 
 +<doodle  
 +title="Do you want str_starts_with, str_starts_with_ci, str_ends_with, and str_ends_with_ci functions in PHP 7.4?" auth="user" voteType="single" closed="yes"> 
 +   * yes 
 +   * no 
 +</doodle> 
 + 
 +---- 
 + 
 +<doodle  
 +title="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?" auth="user" voteType="single" closed="yes"> 
 +   * yes 
 +   * no 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 85: Line 112:
  
 ===== References ===== ===== References =====
-Links to external references, discussions or RFCs+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: [[bugid@50434]].
  
 ===== Rejected Features ===== ===== Rejected Features =====
 Keep this updated with features that were discussed on the mail lists. Keep this updated with features that were discussed on the mail lists.
rfc/add_str_begin_and_end_functions.1471048058.txt.gz · Last modified: 2017/09/22 13:28 (external edit)