Implement ends_with() function in PHP
This article demonstrates how to implement the ends_with() function in PHP.
1. Using strrpos() function
The best option to implement the ends_with() function in PHP is using the strrpos() function. It returns the position of the last occurrence of a substring within a string, where the search starts from the specified offset. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php function ends_with($string, $suffix) { $offset = strlen($string) - strlen($suffix); if ($offset < 0) { return false; } return strrpos($string, $suffix, $offset) === $offset; } $string = "Hello, World"; $suffix = "World"; if (ends_with($string, $suffix)) { echo "'$string' ends with '$suffix'"; } else { echo "'$string' does not end with '$suffix'"; } ?> |
Alternatively, you can use the strpos() function to implement the ends_with() function, which returns the index of the first occurrence of a substring in a string. To match the last occurrence, you can limit the search space using the offset parameter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php function ends_with($string, $suffix) { $offset = strlen($string) - strlen($suffix); if ($offset < 0) { return false; } return strpos($string, $suffix, $offset) === $offset; } $string = "Hello, World"; $suffix = "World"; if (ends_with($string, $suffix)) { echo "'$string' ends with '$suffix'"; } else { echo "'$string' does not end with '$suffix'"; } ?> |
Note: You can make the search case-insensitive using the stripos() and strripos() functions.
2. Using substr() function
The idea is to find the substring length, chop that many characters from the string’s end, and compare the resultant string with the substring. This can be easily done with the substr() function with a negative offset and length omitted, which extracts a substring from the string’s end. The following code example provides a demonstration.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php function ends_with($string, $suffix) { if ($suffix === "") { return true; } return substr($string, -strlen($suffix)) === $suffix; } $string = "Hello, World"; $suffix = "World"; if (ends_with($string, $suffix)) { echo "'$string' ends with '$suffix'"; } else { echo "'$string' does not end with '$suffix'"; } ?> |
Note: This works, but strpos() function is a faster and more memory-efficient option.
3. Using substr_compare() function
Another alternative is to use the substr_compare() function to implement the ends_with() function. It performs a comparison between two strings, starting from an offset, and returns 0 if both strings are equal. To make the comparison case-insensitive, you can pass the substring’s length as the fourth parameter and set the fifth parameter to true.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php function ends_with($string, $suffix) { $offset = strlen($string) - strlen($suffix); return substr_compare($string, $suffix, $offset) === 0; } $string = "Hello, World"; $suffix = "World"; if (ends_with($string, $suffix)) { echo "'$string' ends with '$suffix'"; } else { echo "'$string' does not end with '$suffix'"; } ?> |
4. Using preg_match() function
Finally, you can use a regex to determine whether a string ends with a substring or not. In PHP, the preg_match() function takes a regex, and returns 1 if and only if the pattern matches the string. This can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php function ends_with($string, $suffix) { return preg_match("/{$suffix}$/", $string) === 1; } $string = "Hello, World"; $suffix = "World"; if (ends_with($string, $suffix)) { echo "'$string' ends with '$suffix'"; } else { echo "'$string' does not end with '$suffix'"; } ?> |
Here, $ matches with the end of a string. Since preg_match() function returns false on failure, the use of === operator is preferred. To make the search case-insensitive, you can append i to the pattern, i.e., "/{$suffix}$/i".
That’s all about implementing the ends_with() function in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)