• 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
  • VBA
  • 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.  php array_key_exists() Function – Check if Key Exists in Array
  • 2.  php switch case – How to Use Switch…Case Statements in php
  • 3.  php is_numeric – Check if Variable is a Number
  • 4.  php pi – Get Value of pi Using php pi() Function
  • 5.  php tan – Find Tangent of Number in Radians Using php tan() Function
  • 6.  php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 7.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 8.  php strlen() – Get the Length of String Variable in php
  • 9.  Get Word Count of String in php with str_word_count() Function
  • 10.  php range() – Create Array of Elements Within Given Range

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy