Reputation: 20882
Just looking for a quick way using PHP to check if a string is all numbers and 10 numbers long.
eg:
4343434345 - this would be accepted (10 chars all digits)
343434344 - this wouldn't (9 chars)
sdfefsefsdf - this wouldn't.
thanks
Upvotes: 2
Views: 2470
Reputation: 819
ex:-
if(preg_match('/^\d{10}$/', $str))
echo "A match was found.";
} else {
echo "A match was not found.";
}
Upvotes: 4
Reputation: 254886
if (preg_match('~^\d{10}$~', $str)) { ... }
or
if (ctype_digit($str) && strlen($str) == 10) { ... }
Upvotes: 9