03-Apr-2023
.
Admin
Hi Dev,
This post will give you example of how to check string contains numbers in php?. This article goes in detailed on how do you check if a cell contains a number?. We will use do you check if a string has numbers in php. In this article, we will implement a find a number in a string php?.
There are example to Check if String Contains check string contains numbers in PHP. in this example, we will use to is_numeric() and preg_replace function to check string contains numbers. so Let's the following example with output.
Example 1
index.php
<?php
$string = "1";
if ( is_numeric($string) ) {
echo "\"{$string}\" is a number.";
}else {
echo "\"{$string}\" is not a number.";
}
?>
Output:
"1" is a number.
Example 2
index.php
<?php
// Declare a variable and initialize it
$nice = 'Welcome 2 Nicesnippets 4 Nicesnippets.';
// Filter the Numbers from String
$int_var = preg_replace('/[^0-9]/', '', $nice);
// print output of function
echo("The numbers are: $int_var \n");
?>
Output:
The numbers are: 24
I hope it could help you...
#PHP