This tutorial will discuss about unique ways to check if string starts with a number in php.
Table Of Contents
Method 1: Using Regex
PHP provides a function preg_match()
, it accepts a regex pattern as first parameter and a string as the second parameter. If the string matches the given regex pattern, then it returns 1 otherwise it returns 0.
We can pass a regex pattern i.e. “/^[0-9]/” and given string into the preg_match()
function, and if it returns 1, then it means that the string starts with a number. We have created a function for the same,
function startsWithNumber($str) { return preg_match('/^[0-9]/', $str) === 1; }
This function accepts a string as an argument and returns true if the string starts with the a number.
Let’s see the complete example,
<?php /** * Checks if a string starts with a number using * regular expressions (preg_match()). * * @param string $str The string to check. * @return bool True if the string starts with a number, false otherwise. */ function startsWithNumber($str) { return preg_match('/^[0-9]/', $str) === 1; } $strValue = "221B Baker Street"; // Check if there is a Number at the beginning of String if (startsWithNumber($strValue)) { echo "The string starts with a number."; } else { echo "The string does not start with a number."; } ?>
Output
Frequently Asked:
The string starts with a number.
Method 2: Using is_numeric() & ctype_digit() functions
The is_numeric()
function accepts a variable as an argument and returns true, if the given variable is either in number or a number string. So we can use this to check if the first character of the string is a number or not. But with this, we need also need to use the ctype_digit()
function. It accepts a string as an argument and returns true if all the characters in the provided string are numericals.
So we have created a function which accepts a string as an argument end returns true if the given string starts with a number i.e.
function startsWithNum($str) { $firstChar = $str[0]; return is_numeric($firstChar) && ctype_digit($firstChar); }
Inside this function, we will fetch the first character of the string and then we will use the is_numeric()
and ctype_digit()
functions to check if the string starts with a numeric digit.
In the below example we will use this function to check if the string starts with a given number with any number in PHP
Let’s see the complete example,
<?php /** * Checks if a string starts with a number using * is_numeric() and ctype_digit(). * * @param string $str The string to check. * @return bool True if the string starts with a number, false otherwise. */ function startsWithNum($str) { $firstChar = $str[0]; return is_numeric($firstChar) && ctype_digit($firstChar); } $strValue = "221B Baker Street"; // Check if there is a Number at the beginning of String if (startsWithNum($strValue)) { echo "The string starts with a number."; } else { echo "The string does not start with a number."; } ?>
Output
The string starts with a number.
Summary
Today, we learned about two ways to check if string starts with a Number in PHP.