Check if String Ends With a Number in PHP

This tutorial will discuss about unique ways to check if string ends with a number in php.

Table Of Contents

Method 1: Using Regex

The preg_match() function in PHP, accepts a regular expressions pattern and a string as parameters, and matches the string the given string with the given regex pattern. If the string matches the given regular rexpression, then it returns 1, and 0 if it does not matches the regex pattern, or false on failure.

So, to check if a string ends with a Number, we can use the following regex pattern,

'/\d$/'

This regex pattern, says that the last character of a string must be a digit.

We have created a function to check if a string ends with a Number or not i.e.

function endsWithNumber($str)
{
    return preg_match('/\d$/', $str) === 1;
}

This function uses the above mentioned regex pattern. It accepts a string as argument, and returns True if the given string ends with a number, otherwise it returns false.

Let’s see the complete example,

<?php
/**
 * Checks if a string ends with a number using
 * regular expressions (preg_match()).
 *
 * @param string $str The string to check.
 * @return bool True if the string ends with a number, false otherwise.
 */
function endsWithNumber($str)
{
    return preg_match('/\d$/', $str) === 1;
}

$strValue = "Sample 125";

// Check if string ends with a number
if (endsWithNumber($strValue)) {
    echo "The string ends with a number.";
} else {
    echo "The string does not end with a number.";
}
?>

Output

The string ends with a number.

Method 2: Using is_numeric() and ctype_digit()

The is_numeric() function accepts a variable as argument, and returns if the variable is a number or a numeric string. But if variable is not yet se, then it still returns True. So, to double check, we will use the ctype_digit() function, along with the is_numeric() function. #

The ctype_digit() function accepts a string as argument, and returns true if all the characters in given string are digits.

We have created a function to check if a string ends with a Number or not i.e.

function endsWithNum($str)
{
    $lastChar = $str[strlen($str) - 1];
    return is_numeric($lastChar) && ctype_digit($lastChar);
}

It accepts a string as argument, and returns True if the given string ends with a number, otherwise it returns false.

Let’s see the complete example,

<?php
/**
 * Checks if a string ends with a number using
 * is_numeric() and ctype_digit().
 *
 * @param string $str The string to check.
 * @return bool True if the string ends with a number, false otherwise.
 */
function endsWithNum($str)
{
    $lastChar = $str[strlen($str) - 1];
    return is_numeric($lastChar) && ctype_digit($lastChar);
}

$strValue = "Sample 125";

// Check if string ends with a number
if (endsWithNum($strValue)) {
    echo "The string ends with a number.";
} else {
    echo "The string does not end with a number.";
}
?>

Output

The string ends with a number.

Summary

We learned about two ways to check if a string ends with a Number in PHP.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top