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 Count of Repeating Digits in a Given Number in PHP ?
Counting the number of repeating digits in a given number is a common task in programming. This can be useful in various scenarios, such as data analysis or validation. In this article, we will explore different approaches to counting the repeating digits in a given number using PHP. Table of Conten
5 min read
How to find number of characters in a string in PHP ?
We have given a string and the task is to count number of characters in a string str in PHP. In order to do this task, we have the following methods in PHP: Table of Content Method 1: Using strlen() MethodMethod 2: Using mb_strlen() MethodMethod 3: Using iconv_strlen() MethodMethod 4: Using grapheme
3 min read
How to Check a String Contains at least One Letter and One Number in PHP ?
This article will show you how to check if a string has at least one letter and one number in PHP. Validating whether a string contains at least one letter and one number is a common requirement. In this article, we will explore various approaches to achieve this task in PHP. Table of Content Using
3 min read
How to Check if All Digits of Given Number are Same in PHP ?
Given a number, our task is to check if all the digits of a given number are the same. This can be useful in various scenarios, such as validating user input or processing numerical data. In this article, we will explore different approaches to check if a given number's digits are the same in PHP. E
5 min read
How to extract numbers from string in PHP ?
Extracting numbers from a string in PHP involves isolating numeric values within mixed text. This is essential for data validation, processing, and analysis, allowing developers to retrieve and utilize numerical information from user input, database entries, or external sources, ensuring accurate an
3 min read
PHP Program to Count Digits of a Number
In this article, we will see how to count the digits of a number in PHP. There are three methods to count digits of a number, these are: Table of Content Using strlen() FunctionUsing while LoopUsing str_split() FunctionUsing strlen() FunctionThe strlen() function returns the length of a given string
2 min read
How to check if URL contain certain string using PHP?
Given a URL and the task is to check the URL contains certain string or not. The URL are basically the strings. So in order to check the existence of certain strings, two approaches can be followed. The first approach is used to find the sub string matching in a string and second approach is to find
4 min read
PHP Program to Print All Digits of a Given Number
Given a number, the task is to print all digits of the given number in PHP. There are various methods to print all digits of the given number, we will cover each approach with explanations and examples. Table of Content Using String ConversionUsing Modulus Operator (%)Using RecursionUsing Array and
2 min read
How to get String Length in PHP ?
In this article, we learn how to find the length of the string in PHP. Approach: This task can be done by using the built-in function strlen() in PHP. This method is used to return the length of the string. It returns a numeric value that represents the length of the given string. Syntax: strlen($st
1 min read
PHP to Check if a String Contains any Special Character
Given a String, the task is to check whether a string contains any special characters in PHP. Special characters are characters that are not letters or numbers, such as punctuation marks, symbols, and whitespace characters. Examples: Input: str = "Hello@Geeks"Output: String contain special character
3 min read