Adam
Adam

Reputation: 20882

Check if a string is all numbers and 10 chars long

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

Answers (2)

Sri Tirupathi Raju
Sri Tirupathi Raju

Reputation: 819

ex:-

if(preg_match('/^\d{10}$/', $str))
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

Upvotes: 4

zerkms
zerkms

Reputation: 254886

if (preg_match('~^\d{10}$~', $str)) { ... }

or

if (ctype_digit($str) && strlen($str) == 10) { ... }

Upvotes: 9

Related Questions