To check if a string ends with a particular string in php, for users of php 8.0.0 or later, the easiest way is to use the php str_ends_with() function.

$variable = "I'm a variable";

echo var_dump(str_ends_with($variable, "I'm"));
echo var_dump(str_ends_with($variable, "a variable"));
echo var_dump(str_ends_with($variable, "variable"));

//Output:
bool(false)
bool(true)
bool(true)

If you are using a version of php lower than 8.0.0, you can define your own str_ends_with() function.

function ends_with( $haystack, $needle ) {
    $length = strlen( $needle );
    if( !$length ) {
        return true;
    }
    return substr( $haystack, -$length ) === $needle;
}

$variable = "I'm a variable";

echo var_dump(ends_with($variable, "I'm"));
echo var_dump(ends_with($variable, "a variable"));
echo var_dump(ends_with($variable, "variable"));

//Output:
bool(true)
bool(true)
bool(false)

When working with string variables in our php programs, it is useful to be able to easily extract information about the values of the strings. In php, we can check if a string ends with another string easily.

To check if a string ends with a given string, we can use the str_ends_with() function.

The str_ends_with() function takes two parameters, the string you want to search (the haystack), and the string you want to search for (the needle), and determines if the string you want to search for is at the end of the string you are searching.

Below is a simple example showing how we can use str_ends_with() to check if a string ends with another string.

$variable = "I'm a variable";

echo var_dump(str_ends_with($variable, "I'm"));
echo var_dump(str_ends_with($variable, "a variable"));
echo var_dump(str_ends_with($variable, "variable"));

//Output:
bool(false)
bool(true)
bool(true)

Check if String Ends with Specified String in php before Version 8.0.0

If you are using a version of php that is less than 8.0.0, then you can’t use the built in str_ends_with() function as this was introduced in version 8.0.0.

Instead, you can easily define your own function which checks to see if the string ends with another string using the substr() function.

We will check to see if the substring of length needle from the end is equal to the value of the needle.

Below is the function you can use which is equivalent to str_ends_with() in php version 8.0.0 and some examples.

function ends_with( $haystack, $needle ) {
    $length = strlen( $needle );
    if( !$length ) {
        return true;
    }
    return substr( $haystack, -$length ) === $needle;
}

$variable = "I'm a variable";

echo var_dump(ends_with($variable, "I'm"));
echo var_dump(ends_with($variable, "a variable"));
echo var_dump(ends_with($variable, "variable"));

//Output:
bool(true)
bool(true)
bool(false)

Checking if a String Starts with a Specified String in php with str_ends_with()

If you’d instead like to check if a string starts with a specified string, you can use the str_starts_with() function in a similar way as the str_ends_with() function.

The str_starts_with() function will check to see if the specified string is at the beginning of a string.

Below are a few examples of how to use str_starts_with() to check if a string is at the beginning of another string.

$variable = "I'm a variable";

echo var_dump(str_starts_with($variable, "I'm"));
echo var_dump(str_starts_with($variable, "I'm a"));
echo var_dump(str_starts_with($variable, "variable"));

//Output:
bool(true)
bool(true)
bool(false)

Hopefully this article has been useful for you to learn how to use the php str_ends_with() function to check if a string ends with a given string in php.

Categorized in:

PHP,

Last Update: March 13, 2024