If you just need to check to see if somethings a number:
if(ereg("[0-9]", $variable)) {
return TRUE;
} else {
return FALSE;
}
If you need to check a phone number, this is what I use to check and consistently format North American telephone numbers. It'll return a string formatted as either 555-1212 or (888) 555-1212 if the string parses as a valid North American telephone number. Really. Try it.
function IsUSPhone($v) {
if(ereg("[a-zA-Z]",$v)) {
return false;
}
$v = ereg_replace("[^0-9]","",$v);
switch(strlen($v)) {
case 7:
if(intval(substr($v,0,1)) < 2) {
return false;
}
$phone = substr($v,0,3) . '-' . substr($v,3,4);
break;
case 10:
if(intval(substr($v,0,1)) < 2) {
return false;
}
if(intval(substr($v,3,1)) < 2) {
return false;
}
$phone = '(' . substr($v,0,3) . ') ' . substr($v,3,3) . '-' . substr($v,6,4);
break;
case 11:
if(intval(substr($v,0,1)) != 1) {
return false;
}
if(intval(substr($v,1,1)) < 2) {
return false;
}
if(intval(substr($v,3,1)) < 2) {
return false;
}
$phone = '(' . substr($v,1,3) . ') ' . substr($v,4,3) . '-' . substr($v,7,4);
break;
default:
return false;
break;
}
return $phone;
}