How to check if string contains only numeric digits in PHP ?
Last Updated :
18 Jul, 2024
Given a string, the task is to check whether the string contains only numeric digits in PHP. It is a common task to verify whether a given string consists of only numeric digits in PHP. It can be useful when dealing with user input validation, form submissions, or data processing tasks where numeric values are expected.
For instance, the below examples illustrate the concept:
Input: str = "1234"
Output: true
Input: str = "GeeksforGeeks2020"
Output: false
Here, we will cover three common scenarios for checking if the string contains only numeric digits.
Using ctype_digit Function
PHP provides a built-in function called ctype_digit() that can be used to check if all characters in a string are numeric digits.
Example: This example illustrates checking if string contains only numeric digits using ctype_digit Function in PHP.
PHP
<?php
function isNumeric($str)
{
return ctype_digit($str);
}
// Driver code
$str1 = "12345";
$str2 = "gfg123";
echo isNumeric($str1) ? "Numeric String" : "Non Numeric String";
echo "\n";
echo isNumeric($str2) ? "Numeric String" : "Non Numeric String";
?>
OutputNumeric String
Non Numeric String
Using Regular Expressions
Regular expressions provide a powerful way to validate and manipulate strings. The preg_match() function can be used to check if a string contains only numeric digits.
Example: This example illustrates checking if string contains only numeric digits using the Regular Expressions.
PHP
<?php
function isNumeric($str)
{
return preg_match('/^[0-9]+$/', $str);
}
// Driver code
$str1 = "12345";
$str2 = "abc123";
echo isNumeric($str1) ? "Numeric String" : "Non Numeric String";
echo "\n";
echo isNumeric($str2) ? "Numeric String" : "Non Numeric String";
?>
OutputNumeric String
Non Numeric String
Using is_numeric Function
The is_numeric() function in PHP checks if a variable is a numeric value or a numeric string.
Example: This example illustrates checking if string contains only numeric digits using the is_numeric Function.
PHP
<?php
function isNumeric($str)
{
return is_numeric($str);
}
// Driver code
$str1 = "12345";
$str2 = "abc123";
echo isNumeric($str1) ? "Numeric String" : "Non Numeric String";
echo "\n";
echo isNumeric($str2) ? "Numeric String" : "Non Numeric String";
?>
OutputNumeric String
Non Numeric String
Using filter_var Function
The filter_var() function in PHP can be used with the FILTER_VALIDATE_INT filter to check if a string contains only numeric digits. This method converts the string to an integer and validates it.
Example: This example illustrates checking if a string contains only numeric digits using the filter_var Function.
PHP
<?php
function isNumeric($str)
{
return filter_var($str, FILTER_VALIDATE_INT) !== false;
}
// Nikunj Sonigara
// Driver code
$str1 = "12345";
$str2 = "abc123";
echo isNumeric($str1) ? "Numeric String" : "Non Numeric String";
echo "\n";
echo isNumeric($str2) ? "Numeric String" : "Non Numeric String";
?>
OutputNumeric String
Non Numeric String
Using strspn Function
The strspn() function in PHP returns the length of the initial segment of a string consisting entirely of characters contained within a given mask. We can use this to check if the string contains only numeric digits.
Example:
PHP
<?php
$str = "1234";
if (strspn($str, '0123456789') == strlen($str)) {
echo "true";
} else {
echo "false";
}
?>
Using array_filter and is_numeric
Another valid approach to check if a string contains only numeric digits in PHP is to convert the string into an array of characters and use the array_filter function in combination with is_numeric. This method iterates through each character and verifies if each one is a numeric digit.
Example
PHP
<?php
function isNumericString($str) {
// Convert the string to an array of characters
$charArray = str_split($str);
// Use array_filter to keep only numeric characters
$numericArray = array_filter($charArray, 'is_numeric');
// Check if the filtered array has the same length as the original string
return count($charArray) === count($numericArray);
}
$input1 = "1234";
$input2 = "GeeksforGeeks2020";
echo "Input: $input1\n";
echo "Output: " . (isNumericString($input1) ? 'true' : 'false') . "\n";
echo "Input: $input2\n";
echo "Output: " . (isNumericString($input2) ? 'true' : 'false') . "\n";
?>
OutputInput: 1234
Output: true
Input: GeeksforGeeks2020
Output: false
Similar Reads
How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri
1 min read
How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are
4 min read
How to check if a String contains a Specific Character in PHP ? In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently. Here are some common a
2 min read
How to check for empty string in PHP ? In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.here we have some common approachesTable of ContentUsing empty() functio
4 min read
How to extract Numbers From a String in PHP ? Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some
3 min read
How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to
2 min read
PHP | ctype_digit() (Checks for numeric value) A ctype_digit() function in PHP used to check each and every character of text are numeric or not. It returns TRUE if all characters of the string are numeric otherwise return FALSE. Syntax : ctype_digit(string text) Parameter Used: The ctype_digit() function in PHP accepts only one parameter. text
2 min read
How to Convert a Given Number to Words in PHP? Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to
5 min read
How to Convert Number to Character Array in PHP ? Given a number, the task is to convert numbers to character arrays in PHP. It is a common operation when you need to manipulate or access individual digits of a number. This can be particularly useful in situations where you need to perform operations on the digits of a number, such as digital root
3 min read
How to convert String to Float in PHP ? Converting a string to a float in PHP is a common requirement when handling numeric data stored in string format. This conversion allows the numeric string to be used in calculations or comparisons, ensuring accurate manipulation of data within the program.There are many methods to convert a string
3 min read