- PHP 8.0: New str_starts_with and str_ends_with functions
- Case sensitivity
- Multi-byte strings
- Empty strings
- Polyfills
- Conflicts with current user-land implementations
- Backwards compatibility impact
- str_ends_with
- Parameters
- Return Values
- Examples
- Notes
- See Also
- PHP str_ends_with
- Introduction to the PHP str_ends_with() function
- PHP str_ends_with() function examples
- 1) Using PHP str_ends_with() function with an empty substring example
- 2) Using PHP str_ends_with() function with single character example
- 3) Using the PHP str_ends_with() function with multiple characters example
- 4) Case-sensitive example
- Summary
- PHP RFC: Add str_starts_with() and str_ends_with() functions
- Downsides of Common Userland Approaches
- str_starts_with
- str_ends_with
- Proposal
- Backward Incompatible Changes
PHP 8.0: New str_starts_with and str_ends_with functions
PHP 8.0 comes with two new functions to help you easily assert if a given string is present at the beginning or ending of a haystack string. This goes nicely with str_contains() in PHP 8.0.
- str_starts_with() : Check if a given haystack string starts with the given needle string
- str_ends_with : Check if a given haystack string ends with the given needle string
str_starts_with(string $haystack, string $needle): bool; str_ends_with(string $haystack, string $needle): bool;
PHP 8 finally brings the total number of string-related functions in PHP to over 100, and the new functions can be easily mimicked with existing functions such as strpos , substr , strncmp , and substr_compare . However, these new functions were well-received due to possible engine-level optimizations and their frequent use-cases.
Case sensitivity
Both str_starts_with() and str_ends_with() functions are case-sensitive. There are no flags or other functions to make them case-insensitive. This is the same pattern with str_contains
Multi-byte strings
Multi-byte ( mb_* ) variants for str_starts_with() and str_ends_with() are not currently planned.
Empty strings
Similar to str_contains , PHP now considers empty string ( «» ) to be present in everywhere in a string. To quote Nikita:
As of PHP 8, behavior of » in string search functions is well defined, and
we consider » to occur at every position in the string, including one past
the end. As such, both of these will (or at least should) return true. The
empty string is contained in every string.
The following calls will be always true:
str_starts_with('Foo', ''); // true str_starts_with('', ''); // true str_ends_with('Foo', ''); // true str_ends_with('', ''); // true
Polyfills
Here is a polyfill for PHP 7.0 and later. Be sure to wrap them with function_exists() calls where you use them. These functions pass the exact same tests in PHP core.
function str_starts_with(string $haystack, string $needle): bool < return \strncmp($haystack, $needle, \strlen($needle)) === 0; >function str_ends_with(string $haystack, string $needle): bool
Conflicts with current user-land implementations
Starts-with and ends-with functionality is often provided as helper functions in various frameworks. This includes Symfony String component, Laravel Str helper, and Yii StringHelper .
There are over 4,000 str_starts_with() matches on GitHub, for PHP, most of which appear to be already namespaced.
Case-insensitivity support | Empty strings at every position | |
---|---|---|
PHP 8.0 str_starts_with str_ends_with | No | Yes |
Polyfill (above) str_starts_with str_ends_with | No | Yes |
Symfony String ::startsWith ::endsWith | Yes With ::ignoreCase() | No |
Laravel Str::startsWith Str::endsWith | No | No |
Yii StringHelper::startsWith StringHelper::endsWith | Yes (default) With parameter | No |
Backwards compatibility impact
Both str_starts_with and str_ends_with functions are new functions. Unless you already have a str_contains() function declared, there should be no BC impact.
PHP 8’s new behavior that it considers there is an empty string at every position of a string can be tricky. Note that Laravel helpers and Symfony String component, among many others return false when you search for an empty string needle ( «» ) at the start and end of strings, although PHP core returns true .
© 2018-2023 PHP.Watch, with ❤ from Ayesh • About PHP.Watch
str_ends_with
Performs a case-sensitive check indicating if haystack ends with needle .
Parameters
The substring to search for in the haystack .
Return Values
Returns true if haystack ends with needle , false otherwise.
Examples
Example #1 Using the empty string »
The above example will output:
All strings end with the empty string
Example #2 Showing case-sensitivity
$string = ‘The lazy fox jumped over the fence’ ;
?php
if ( str_ends_with ( $string , ‘fence’ )) echo «The string ends with ‘fence’\n» ;
>
if ( str_ends_with ( $string , ‘Fence’ )) echo ‘The string ends with «Fence»‘ ;
> else echo ‘»Fence» was not found because the case does not match’ ;
>
The above example will output:
The string ends with 'fence' "Fence" was not found because the case does not match
Notes
Note: This function is binary-safe.
See Also
- str_contains() — Determine if a string contains a given substring
- str_starts_with() — Checks if a string starts with a given substring
- stripos() — Find the position of the first occurrence of a case-insensitive substring in a string
- strrpos() — Find the position of the last occurrence of a substring in a string
- strripos() — Find the position of the last occurrence of a case-insensitive substring in a string
- strstr() — Find the first occurrence of a string
- strpbrk() — Search a string for any of a set of characters
- substr() — Return part of a string
- preg_match() — Perform a regular expression match
PHP str_ends_with
Summary: in this tutorial, you’ll learn how to use the PHP str_ends_with() function to check if a string ends with a substring.
Introduction to the PHP str_ends_with() function
The str_ends_with() function performs a case-sensitive search and checks if a string ends with a substring.
Here’s the syntax of the str_ends_with() function:
str_ends_with ( string $haystack , string $needle ) : bool
Code language: PHP (php)
The str_ends_with() function has two parameters:
- $haystack is the input string to check.
- $needle is the substring to search for in the input string.
The str_ends_with() function returns true if the $haystack ends with the $needle or false otherwise.
Note that every string ends with an empty string. Therefore, if the $needle is an empty ( » ), the str_ends_with() always returns true .
The str_ends_with() function has only been available since PHP 8.0.0. If you use PHP with a lower version, you can polyfill the function like this:
if (!function_exists('str_ends_with')) < function str_ends_with($haystack, $needle) < return $needle !== '' ? substr($haystack, -strlen($needle)) === $needle : true; > >
Code language: PHP (php)
PHP str_ends_with() function examples
Let’s take some examples of using the PHP str_ends_with() function.
1) Using PHP str_ends_with() function with an empty substring example
The following example returns true because every string ends with an empty string:
echo str_ends_with('Hello there', '');
Code language: PHP (php)
2) Using PHP str_ends_with() function with single character example
The following example uses the PHP str_ends_with() function to check if the string ‘PHP tutorial’ ends with the letter ‘l’ :
$str = 'PHP tutorial'; $substr = 'l'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result ending with $substr";
Code language: PHP (php)
The PHP tutorial is ending with l
Code language: PHP (php)
3) Using the PHP str_ends_with() function with multiple characters example
The following example uses the PHP str_ends_with() function to check if the string ‘PHP tutorial’ ends with the string ‘tutorial’ :
$str = 'PHP tutorial'; $substr = 'tutorial'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result ending with $substr";
Code language: PHP (php)
The PHP tutorial is ending with tutorial
Code language: PHP (php)
4) Case-sensitive example
It’s important to notice that the str_ends_with() function carries a case-sensitive search. For example:
$str = 'PHP tutorial'; $substr = 'Tutorial'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";
Code language: PHP (php)
The PHP tutorial is not ending with Tutorial
Code language: PHP (php)
Summary
- Use the PHP str_ends_with() function to perform a case-sensitive search and check if a string ends with a substring.
PHP RFC: Add str_starts_with() and str_ends_with() functions
str_starts_with checks if a string begins with another string and returns a boolean value ( true / false ) whether it does.
str_ends_with checks if a string ends with another string and returns a boolean value ( true / false ) whether it does.
Typically this functionality is accomplished by using existing string functions such as substr , strpos / strrpos , strncmp , or substr_compare (often combined with strlen ). These bespoke userland implementations have various downsides, discussed further below.
The str_starts_with and str_ends_with functionality is so commonly needed that many major PHP frameworks support it, including Symfony, Laravel, Yii, FuelPHP, and Phalcon 1) .
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
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).
Additionally, there are performance issues with many userland implementations.
Note: some implementations add “ $needle === «» || ” and/or “ strlen ( $needle ) ” guards to handle empty needle values and/or avoid warnings.
str_starts_with
substr($haystack, 0, strlen($needle)) === $needle
This is memory inefficient because it requires an unnecessary copy of part of the haystack.
This is potentially CPU inefficient because it will unnecessarily search along the whole haystack if it doesn’t find the needle.
strncmp($haystack, $needle, strlen($needle)) === 0 // generic strncmp($subject, "prefix", 6) === 0 // ad hoc
This is efficient but requires providing the needle length as a separate argument, which is either verbose (repeat “ $needle ”) or error prone (hard-coded number).
str_ends_with
substr($haystack, -strlen($needle)) === $needle
This is memory inefficient (see above).
strpos(strrev($haystack), strrev($needle)) === 0
This is doubly inefficient because it requires reversing both the haystack and the needle as well as applying strpos (see above).
strrpos($haystack, $needle) === strlen($haystack) - strlen($needle)
This is verbose and also potentially CPU inefficient.
substr_compare($haystack, $needle, -strlen($needle)) === 0 // generic substr_compare($subject, "suffix", -6) === 0 // ad hoc
This is efficient but either verbose or error prone (see strncmp above).
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.
$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";
Note: the behavior concerning empty strings is in accordance with what is described in the accepted str_contains RFC. This behavior is also the same as is common with other languages, including Java and Python.
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 it does not justify the rejection of this RFC .