Check for numbers using Regular Expressions

Check for numbers only using regular expressions

<?php
// sample strings
$string1 = “123”;
$string2 = “abc123”;
//check string 1
if (ereg(‘[^0-9]’, $string1))
{
  echo “characters and numbers”;
}
else
{
  echo “numbers only”;   
}
//check string2
if (ereg(‘[^0-9]’, $string2))
{
  echo “characters and numbers”;
}
else
{
  echo “numbers only”;    
}

?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*