127
votes

The standard PHP way to test whether a string $str ends with a substring $test is:

$endsWith = substr( $str, -strlen( $test ) ) == $test

Is this the fastest way?

13
PHP 8.0 introduces new method for this job str_end_with: stackoverflow.com/a/64160081/7082164 - Jsowa

13 Answers

153
votes

What Assaf said is correct. There is a built in function in PHP to do exactly that.

substr_compare($str, $test, strlen($str)-strlen($test), strlen($test)) === 0;

If $test is longer than $str PHP will give a warning, so you need to check for that first.

function endswith($string, $test) {
    $strlen = strlen($string);
    $testlen = strlen($test);
    if ($testlen > $strlen) return false;
    return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}
68
votes

This method is a tiny bit more memory-expensive, but it is faster:

stripos(strrev($haystack), $reversed_needle) === 0;

This is best when you know exactly what the needle is, so you can hard-code it reversed. If you reverse the needle programatically, it becomes slower than the earlier method.

63
votes
$endsWith = substr_compare( $str, $test, -strlen( $test ) ) === 0

Negative offset "starts counting from the end of the string".

11
votes

Here’s a simple way to check whether one string ends with another, by giving strpos an offset right where the string should be found:

function stringEndsWith($whole, $end)
{
    return (strpos($whole, $end, strlen($whole) - strlen($end)) !== false);
}

Straightforward, and I think this’ll work in PHP 4.

8
votes

It depends on which sort of efficiency you care about.

Your version uses more memory due to the extra copy from the use of substr.

An alternative version might search the original string for the last occurrence of the substring without making a copy, but would probably be slower due to more testing.

Probably the most efficient way is to do loop char-by-char from the -sterlen(test) position till the end of the string and compare. That's the minimal amount of comparisons you can hope to do and there's hardly any extra memory used.

5
votes

Another way would be to use the strrpos function:

strrpos($str, $test) == strlen($str) - strlen($test)

But that’s not faster.

5
votes

In PHP 8:

str_ends_with('haystack', 'stack'); // true
str_ends_with('haystack', 'K'); // false

and also:

str_starts_with('haystack', 'hay'); // true

PHP RFC: Add str_starts_with(), str_ends_with() and related functions

3
votes

I hope that the below answer may be efficient and also simple:

$content = "The main string to search";
$search = "search";
//For compare the begining string with case insensitive. 
if(stripos($content, $search) === 0) echo 'Yes';
else echo 'No';

//For compare the begining string with case sensitive. 
if(strpos($content, $search) === 0) echo 'Yes';
else echo 'No';

//For compare the ending string with case insensitive. 
if(stripos(strrev($content), strrev($search)) === 0) echo 'Yes';
else echo 'No';

//For compare the ending string with case sensitive. 
if(strpos(strrev($content), strrev($search)) === 0) echo 'Yes';
else echo 'No';
1
votes

Don't know if this is fast or not but for a single character test, these work, too:

(array_pop(str_split($string)) === $test) ? true : false;
($string[strlen($string)-1] === $test) ? true : false;
(strrev($string)[0] === $test) ? true : false;
1
votes

easiest way to check it via regular expression

for example to check if the mail given is gmail:

echo (preg_match("/@gmail\.com$/","[email protected]"))?'true':'false';
0
votes

I'm thinking the reverse functions like strrchr() would help you match the end of the string the fastest.

0
votes

This is pure PHP, without calling external functions, except for strlen.

function endsWith ($ends, $string)
{
    $strLength = strlen ($string);
    $endsLength = strlen ($ends);
    for ($i = 0; $i < $endsLength; $i++)
    {
        if ($string [$strLength - $i - 1] !== $ends [$i])
            return false;
    }
    return true;
}
-1
votes

for single-char needle:

if (@strrev($haystack)[0] == $needle) {
   // yes, it ends...
}