• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Check if String Ends with Given String in php with str_ends_with()

Check if String Ends with Given String in php with str_ends_with()

March 18, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  In PHP isset Method is Used For Checking if Variable is Defined
  • 2.  php array_filter() – Filter Array Elements with Function in php
  • 3.  php in_array – Check If Value is in php Array
  • 4.  php range() – Create Array of Elements Within Given Range
  • 5.  php str_replace – Replace String in php
  • 6.  php array_merge() – Merge One or More Arrays Together by Appending
  • 7.  php Absolute Value with abs Function
  • 8.  php array_diff() – Find Elements of Array Not Present in Other Arrays
  • 9.  How to Get Current System IP Address in php
  • 10.  php cosh – Find Hyperbolic Cosine of Number Using php cosh() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy